Findbyattributes() Not Using Limit 1

I have a huge DB, and I was using findByAttributes() to get the first record of an ordered set, with a joined table. It started failing with a fatal error that I’d maxed out my memory… The fix was to manually add LIMIT 1 to my custom scope.

Seems like it was trying to select 20,000+ rows, join another table, sort the rows, pull them all back into PHP, then just return the first one. Am I correct in understanding that findByAttributes() et al. doesn’t automatically use LIMIT 1? Is there a reason for this?

Just curious, thanks!


From looking at the code behind the CActiveRecord::query() method, it seems that the limit condition is added as long as there is no with clause (so there are no joins).

Perhaps this is because adding a limit clause is too naïve an approach when complex joins are involved, but I can’t immediately see why that would be.


It could of course be a bug. Would you like me to move this thread into the bug discussion forum?

Yeah, I think I would. It seems like a bug / unexpected behavior.


$comments= BlogComment::model()->with(‘fkPost’)->findAllByAttributes(array(‘fk_post_id’=>$post->post_id,‘c_is_enabled’=>1,‘c_is_deleted’=>0,array(‘limit’=>2)));

on above query getting an error:Table "blog_comment" does not have a column named "0".

any body tell me how to use limit with findByAttributes???? :rolleyes:

Totally wrong thread. Go ask somewhere else, or better yet do a google search on CActiveRecord limit

This is a bug report forum, not a Q&A forum.