You guys are not the only ones. Working with relations in Yii is challenging to say the least. While the documentation is good, it doesn’t provide examples for common scenarios and use cases.
Let’s say you have three models: Post, Author, and Comment. Each Post has one Author. Each Comment belongs to a Post.
To fetch all Posts with Authors and Comments using eager loading:
$criteria = new CDbCriteria();
$criteria->together = true; // <-- If 'on' used in join, 'together' should be removed.
$criteria->with = array('authors', 'comments');
$model = Post::model()->findAll($criteria);
I just rethought about this problem. and I have to point out, if with(‘relation’)->findAll() failed to perform eager loading, then the documentation(‘Relational Active Record’) is misleading. maybe somebody from dev team can clarify this issue? thanks a lot.