问题:
我有两种型号:经销商,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' => ''
),
);
答案 0 :(得分:1)
the documentation中有(现在)这样的例子。应用于问题中的示例:
class TestDrive extends AppModel {
public $belongsTo = array(
'Dealer' => array(
'counterCache' => array(
'active' => array(
'TestDrive.active' => 1
),
'processed' => array(
'TestDrive.active' => 1,
'TestDrive.processed' => 1
)
)
)
);
}
请注意,无需使用默认值(className
,foreignKey
等)定义键。)