views:

95

answers:

1

In a simple CakePHP model where User hasMany Item (and Item belongsTo User)- Suppose 50 users, have each 10 items. How can I find() only 5 users, each with the latest 5 items he has? When I impose a limit in the find, it only limits the number of users, not the number of associated Items.

Thanks!

+1  A: 

Just spec the limit attribute in your hasMany model declaration.

var $hasMany = array(
    'Comment' => array(
        'className'     => 'Comment',
        'foreignKey'    => 'user_id',
        'conditions'    => array('Comment.status' => '1'),
        'order'    => 'Comment.created DESC',
        'limit'        => '5',
        'dependent'=> true
    )
);  

(Appropriate props to the CakePHP Book page that I stole this code from.)

Dan Berlyoung
Thanks Dan. I must have skipped this part of the docs :)
azv