I can't get Stat Relation queries to eager load.

As I recall you used to not be able to eager load them, but if you read the following from the Definitive guide it now says you can eager load stat queries:

http://www.yiiframework.com/doc/guide/1.1/en/database.arr

Am I the only one that can’t get this to work?

I am having the same problem. even thought I use ‘with’ like:


$comment::model->with('author','children')->findAll();

I don’t get the children comments back. only when I loop throught the comment,did I get the children.

I also tried together() like:


$comment::model->with('author','children')->together()->findAll();

but still doesn¨t help. can some body gives some pointer here. why eager loading is not working. I am using Yii 1.18. thanks a lot

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);



Hope this helps.

it works, thanks a lot. scott.

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.

I also have problem in eager loading of stat query (criteria), I have used the "together" property of criteria and

observed that eager loading worked for non-stat relations, but for stat relation it is not working.

Is it that eager loading can not be expected for stat relations?

If you have any solution please share.

Thanks in advance.

Following is the code for criteria




 $criteria->with = array(


            // 1. Non-STAT relation

            'pl_sports' => array('joinType'=>'LEFT JOIN',

            ),

          

            // 2. STAT relation 

            'total_rating' => array(

                'joinType'=>'LEFT JOIN'

            )

        );




  $criteria->together = true; // Enables eager loading for relation 1 only