问题:
我有两个模型:经销商,Testdrive(Testdrive 通过dealer_id 属于经销商)。我想显示有关经销商的实时统计信息:总计 (Testdrive.active = 1)、已处理 (Testdrive.active = 1 && Testdrive.processed = 1) ...
我有大约 100 名经销商和 10000 次试驾。基于计数的 sql 大约需要 10 秒(效率低)。现在我有一个每小时运行一次的 cronjob,但我没有实时统计数据。
我试过这样的事情:
var $belongsTo = array(
'Dealer' => array(
'className' => 'Dealer',
'foreignKey' => 'dealer_id',
'counterCache' => 'active',
'counterScope' => array('Testdrive.active' => 1),
'conditions' => '',
'fields' => '',
'order' => ''
),
'Dealer' => array(
'className' => 'Dealer',
'foreignKey' => 'dealer_id',
'counterCache' => 'processed',
'counterScope' => array('Testdrive.active' => 1, 'Testdrive.processed' => 1),
'conditions' => '',
'fields' => '',
'order' => ''
)
);
...但我覆盖了 belongsTo => 'Dealear' 值。
我可以拥有一个带有 counterScope 数组的 counterCache 数组吗?
var $belongsTo = array(
'Dealer' => array(
'className' => 'Dealer',
'foreignKey' => 'dealer_id',
'counterCache' => array('active', 'processed'),
'counterScope' => array('active' => array('Testdrive.active' => 1), 'processed' => array('Testdrive.active' => 1, 'Testdrive.processed' => 1)),
'conditions' => '',
'fields' => '',
'order' => ''
),
);