Is there any way to apply limit directly to the Active Record and not write a criteria again?
In the case i raised in the title, user is the current model, and a user HAS_MANY posts, but if i want a limit I have to revise
How to apply limit directly to $user->posts?
you can add a default scope in your model with the limit. To learn about default scope, checkout the definitive guide -
You might also want to checkout named scopes and parameterized scopes.http://www.yiiframework.com/doc/guide/1.1/en/database.ar#named-scopes
Alright, thank you guys.
But I still have to write a lot through this method(Actually, in some condition, it’s better write a criteria). I think Yii should add $user->post->limit(0,10) feature. More reusable, more convenient.
i don’t get it. You said you didn’t want the code to repeat and that ‘limit’ should be applied by default. scopes are perfect for that.
then you say you want something like model()->findall()->limit(). Well, you can do that within findAll - findAll(array(‘limit’=>10))
Yii gives you the flexibility to formulate the same query multiple ways for different scenarios.