我有一个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