Arbel Zapf Fri Apr 26 04:41:17 -0400 2013

Subject: has_many only works for first result?

Hello everyone.

I'm currently trying to get relationships going and apparently, I'm being utterly stupid. I know this since I'm basically doing the exact same thing as in the Documentation:

class Author extends ActiveRecord\Model {

static $has_many = array(
array('posts')
);

}

class Post extends ActiveRecord\Model {
static $has_one = array(
array('author')
);

}

Now if I do:

Post::find('all', array('limit' => 10, 'include' => array('author')));

foreach ($posts as $post)
echo $post->author->name;

...it will display the name of the author with every post, but only for the first of every author.
So if there are 3 posts by user Foo, followed by 2 posts of user Bar, I will get something like this:

"Post1" - by Foo;

"Post2" - by Notice: Trying to get property of non-object (...)

"Post3" - by Notice: Trying to get property of non-object (...)

"Post4" - by Bar;

"Post5" - by Notice: Trying to get property of non-object (...)

It does not matter if I eager load the association or not. What am I doing/thinking wrong?