kla/php-activerecord — pull request #256 by anther (2013-02-07 15:32:06+00:00)
Title: Fix for triple level includes in the options array

(Reposted because I did the original pull request on the wrong branch)

I posted this as a question on Stackoverflow about a half year ago, and was suggested a fix that I've been using since then, and feel it'd be appropriate for the fix to be included in the base repo. http://stackoverflow.com/questions/9983803/triple-level-associations-in-phpactiverecord

Essentially if you go down to a third level of eager loaded associations

$criteria['include'] = array('playlist_songs'=>array('song'=>array('album')));
//or
$criteria['include'] = array('events'=>array('host'=>array('events')));
You'd end up with this error instead of the requested data: Undefined offset: 0

The problem was traced to the Table::execute_eager_load()
at line 257: $nested_includes = count($name) > 1 ? $name : $name[0];
which is fixed by changing it to $nested_includes = count($name) > 0 ? $name : $name[0];

Which is essentially saying, if the array is 2 or more, return the first element, but if the count is equal to 1, don't treat it as an array, even though it still is an array at that point.

I added a test to RelationshipTest.php that fails before the change.