在CakePHP 3中,我有一個名爲Articles的模型和一個名爲'subject'的字段,我遇到了一個試圖檢索100個最常用文章主題列表的路障。如何在CakePHP 3中使用COUNT(*)與find('list')?
下面的代碼的生成的SQL選擇所有可能的領域和不COUNT(*)
:
$articles->find('list', [
'keyField' => 'subject',
'valueField' => 'COUNT(*)'
])
->group('subject')
->order(['COUNT(*)' => 'DESC'])
->limit(100)
->toArray();
然後我想起"CakePHP’s ORM offers abstraction for some commonly used SQL functions."。但是,下面的代碼導致「錯誤:函數名稱必須是一個字符串」:
$countFunc = $this->find()->func()->count('*');
$articles->find('list', [
'keyField' => 'subject',
'valueField' => $countFunc
])
->group('subject')
->order([$countFunc => 'DESC'])
->limit(100)
->toArray();
$ this-> Zendesks-> find('list',['keyField'=>'company_id','valueField'=>'comment_id']) - > where(['created_at> ='=> $ month3Start,'created_at <=' => ('''']]) - > group('company_id($''')] - > select(['company_id','comment_id'=> $ this-> Zendesks-> find 「); 只需在這裏複製我的作品,這可能會幫助他人瞭解更多細節。並感謝您的代碼! –