我有一個CakePHP 3應用程序,它有2個模型,Urls
和Downloads
。這些模型是相關聯的,使得URL可以有多個下載(Cake中的Urls hasMany Downloads
)。如何在CakePHP 3中分頁查詢子查詢?
當我做一個查詢像這樣它就會從Urls
表中返回1行以及所有相關Downloads
的:
// This is in a view() method
$query = $this->Urls->find()->contain(['Downloads' => ['sort' => ['Downloads.created' => 'DESC'] ] ])->where(['id' => $id]);
我想分頁的Downloads
列表中,但無法看到如何做這個。我已經看到了在https://book.cakephp.org/3.0/en/controllers/components/pagination.html但我唯一能找到的東西,我試過是:
public function initialize()
{
parent::initialize();
$this->loadComponent('Paginator');
}
// In my view() method
$this->paginate = [
'contain' => ['Downloads']
];
$query = $this->Urls->find()->contain(['Downloads' => ['sort' => ['Downloads.created' => 'DESC'] ] ])->where(['id' => $id])->paginate();
,但它只是錯誤說 未知法「拼版」
您可能希望再次仔細閱讀文檔,'paginate()'方法不是表類的方法。 – ndm