About Relational AR

Here is example from guide

class User extends CActiveRecord


    public function relations()


        return array(

            'posts'=>array(self::HAS_MANY, 'Post', 'authorID'

                            'order'=>'??.createTime DESC',


            'profile'=>array(self::HAS_ONE, 'Profile', 'ownerID'),




i'm trying to get the same result. i've news category, news list and authors. i select some category and i need to load all news with authors.

where i need to put authors relations? in news ar model?

here is my code:

category relations

'newslist'=>array(self::MANY_MANY, 'news', 'NewsCategory(categoryId, newsId)','with'=>'author')

news relations

'author'=>array(self::BELONGS_TO, 'Users', 'authorId')

but i get only selected category with news and without authors…


sry for bad english  ;D

this variant does'n work for me too

$posts=Category::model()->with(array('newslist' => array('with' => 'author')))->find($criteria);

I think, this has been changed in 1.0.2 to:


well, maybe yes, but i need to load newslist and newslist.author

You get both with the statement above.


foreach ($posts as $post) {

  echo $post->newslist->name;

  echo $post->newslist->author->name;


But your relations have to be correct first, of course. Didn’t have a look at them ;)

WOW!!! great, it’ working!!  :D

many thanks!!

mmm… and how can i set wich fields to select?

Have a look at CActiveRecord::with() API doc, there’s an example.