我正在试图获得每个商店的评论数量,我真的坚持这一步 是否可以这样做,以及如何做到?
public function display()
{
$this->loadModel('Shops');
$shoplist=$this->Shops->find('all')->order(['title' => 'DESC', 'id' => 'ASC'])->contain([
'Comments' => function ($q) {
return $q->select('id')->where(['published' => 1])->count();
}]);;
$this->set('shoplist', $shoplist );
}
答案 0 :(得分:0)
您需要的是 CounterCache行为
http://book.cakephp.org/3.0/en/orm/behaviors/counter-cache.html