Search code examples
associationseager-loadingphpactiverecord

Triple level associations in phpactiverecord


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.


Solution

  • 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;
    }