如何从关联的模型限制的记录数 [英] How to limit number of records from associated model
本文介绍了如何从关联的模型限制的记录数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在一个简单的CakePHP的模型,其中用户的hasMany项目(项目和用户的belongsTo) -
假设50个用户,每个有10个项目。
我如何才能找到()仅5个用户,每一个最新的5个项目他有?
当我在查找强加一个限制,它只能限制用户的数目,而不是关联的物品的数量。
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.
推荐答案
只需SPEC在限制属性,您的hasMany模型声明。
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
)
);
(相应的道具来,我偷了从这个code中的 CakePHP的书页。)
这篇关于如何从关联的模型限制的记录数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文