Yii Framework Forum: Findbyattributes() Not Using Limit 1 - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Findbyattributes() Not Using Limit 1 Or am I misunderstanding? Rate Topic: -----

#1 User is offline   illustrationism 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 09-April 13

Posted 14 April 2014 - 11:22 AM

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!

D
0

#2 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,502
  • Joined: 04-March 10
  • Location:UK

Posted 14 April 2014 - 11:41 AM

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.

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

#3 User is offline   illustrationism 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 09-April 13

Posted 14 April 2014 - 11:59 AM

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

Thanks.



View PostKeith, on 14 April 2014 - 11:41 AM, said:

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.

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

0

#4 User is offline   vijay24 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 18-January 14

Posted 16 April 2014 - 12:29 AM

$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:
0

#5 User is offline   illustrationism 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 09-April 13

Posted 16 April 2014 - 10:05 AM

View Postvijay24, on 16 April 2014 - 12:29 AM, said:

...
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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users