您可以使用COunterCache来做到这一点......唯一的是,您应该为每个活动定义单独的belongsTo关系。首先我要说,这不是在实践中解决这个问题的最好笏,而是因为你问:
var $belongsTo = array(
'UserWalk' => array(
'counterCache' => true,
'foreignKey' => 'user_id',
'className' => 'User',
'conditions' => array('Activity.type' => 'walk'),
'counterScope' => array('Activity.type' => 'walk')
),
'UserRun' => array(
'counterCache' => true,
'foreignKey' => 'user_id',
'className' => 'User',
'conditions' => array('Activity.type' => 'run'),
'counterScope' => array('Activity.type' => 'run')
),
);
之后,蛋糕将寻找计数字段每个相关模型。 counterChache的fieldsnaming现在应该是像user_run_count或userrun_count(我不知道这个约定)
好吧我怎么会只计算一种类型? – pll
counterScope:http://book.cakephp.org/view/1033/counterCache-Cache-your-count –
另外,有没有办法改变默认的counter列的用法?如果我有一个“活动”表,我筛选散步,我可以在“用户”计数器缓存更新walk_count,而不是被迫使用“activity_count”? – pll