3

问题:

我有两个模型:经销商,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' => ''
    ),
);
4

1 回答 1

1

(现在)文档中有一个这样的例子。应用于问题中的示例:

class TestDrive extends AppModel {

    public $belongsTo = array(
        'Dealer' => array(
            'counterCache' => array(
                'active' => array(
                    'TestDrive.active' => 1
                ),
                'processed' => array(
                    'TestDrive.active' => 1, 
                    'TestDrive.processed' => 1
                )
            )
        )
    );
}

请注意,无需使用默认值(等)定义classNameforeignKey

于 2013-03-13T20:05:02.817 回答