Triple level associations in phpactiverecord

守給你的承諾、 提交于 2019-12-12 02:55:07

问题


Right now if I try to eager load more than two objects deep in phpactiverecord, I get an error.

Is something such as this:

$conditions['include'] = array( 'playlists' => array('playlist_songs' =>array('song')));
User::find('first', $conditions);

Just one level too much to try to retrieve? I'm getting an error Undefined offset: 0 whenever I try to use an association 3 levels deep. Thanks for any help or insight :D.

Edit:

So I've found a pattern that's a little odd. If I have array('playlist_songs'=>array('song'=>array('album')));, the eager load will break for me. But if I add another association to the array, it then works correctly. array('playlist_songs'=>array('song','song','song','song'=>array('album')));

I used 'song' multiple times in that array just to make the fix very apparent.


回答1:


Just fixed this for myself by changing line 247 of Table.php in execute_eager_load from

if (is_array($name))
{
            $nested_includes = count($name) > 1 ? $name : $name[0];//<-- this line
            $name = $index;
}

to

if (is_array($name))
{
            $nested_includes = count($name) > 0 ? $name : $name[0];
            $name = $index;
}



回答2:


This should work. An example I've just tested this my own code:

$model = MyItem::find('first',array('include' => array('Group' => array('SomeGroupFeature'))));

The only thing I can think of is that you don't have any results in one of the things, or your conditions contains something else from a previous use of the array?



来源:https://stackoverflow.com/questions/9983803/triple-level-associations-in-phpactiverecord

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!