Limit gets ignored

Hello.

I’ve just encountered the problem that Yii ignores my limit in my SQL statement. The forum search showed me that I have to disable pagination. But I use CDbCriteria object, so it may indeed not related to it, right? Here is my code up to now. If I display the SQL statement, I can not see any limit.


$folders = Folder::model()->with(array(

    ...

    'posts'=>array(

        ...

        'limit'=>3,

    ),

))->findAll();

Thanks for your help! :)

Try to write the same query in plain SQL and you’ll understand immediately why it’s not possible :)

I see your point. The problem is that I can not write one single sql-statement which should get all folders and posts with a limit. So you say that the problem is that I am using eager loading instead of lazy loading, right?

How can I use lazy loading than? I can not simply remove the "posts" from "folders", because there is another code in there that I need.

Thank you!

This problem has been discussed in this forum (I can’t find the topic) and it seems that the “n+1 query problem” can’t be avoided in this situation :(




foreach ($folders as $folder) {

	$posts = $folder->posts(array('limit' => 3));

	//...

}



give ‘offset’

try


$posts = $folder->posts(array('offset'=>0,'limit' => 3));



It won’t help.

I’ve had the same problem, the solution is not to use “with”. Just skip “posts” in “with” statement and it’ll apply the limit defined in relation.