在处理HABTM时,我很难理解Hash::extract的语法。
我有一个find()返回的数据,看起来像这样:
array(
(int) 0 => array(
'EventsGroup' => array(
'id' => '34',
'event_id' => '5',
'group_id' => '1'
)
),
(int) 1 => array(
'EventsGroup' => array(
'id' => '29',
'event_id' => '2',
'group_id' => '1'
)
)
)我试图得到一个数组,它看起来像:array(x,y,z),其中x,y,z是event_id's。
Cake文档的示例如下所示:
$users = $this->User->find("all");
$results = Hash::extract($users, '{n}.User.id');基于此,我尝试了:
$eventsGroups = $this->EventsGroup->findAllByGroupId($groupid);
$secEvents = Hash::extract($eventsGroups, '{n}.{EventsGroup}.event_id' );
$secEvents2 = Hash::extract($eventsGroups, '{n}.EventsGroup.event_id' );
$secEvents3 = Hash::extract($eventsGroups, '{n}.[text=EventsGroup].event_id);这些都不管用。
我找到了一种不用使用Hash::extract就能得到我想要的东西的方法,但我想使用它,因为其他一些方法将来也会对我有用。
如有任何帮助或建议,我们将不胜感激!
谢谢
发布于 2013-09-07 02:08:59
您是否尝试过:
$eventsGroups = $this->EventsGroup->findAllByGroupId($groupid);
$secEvents = Hash::extract($eventsGroups, '{n}.EventsGroup[event_id]' );正如docs所说,要检索字段,应该使用匹配器,而不是表达式。并且括号({})不是必需的,它们用于构建像‘{n}.{s}’这样的表达式...
https://stackoverflow.com/questions/13465552
复制相似问题