在 CakePHP3 中构建查询
Build query at CakePHP3
我正在尝试获取每个商店的评论数,但我真的卡在了这一步。
有没有可能,怎么做?
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 );
}
您需要的是 CounterCache 行为
http://book.cakephp.org/3.0/en/orm/behaviors/counter-cache.html
我正在尝试获取每个商店的评论数,但我真的卡在了这一步。
有没有可能,怎么做?
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 );
}
您需要的是 CounterCache 行为
http://book.cakephp.org/3.0/en/orm/behaviors/counter-cache.html