如何在cakephp 3中对数组查询结果进行分页 您好,我有一个要分页的查询结果数组。但不知怎的,我得到了一个错误,上面写着:
无法找到与paginate兼容的对象。 以下是我当前的代码:如何在cakephp 3中对数组查询结果进行分页 您好,我有一个要分页的查询结果数组。但不知怎的,我得到了一个错误,上面写着:,cakephp,pagination,cakephp3,Cakephp,Pagination,Cakephp3,无法找到与paginate兼容的对象。 以下是我当前的代码: $this->paginate = ['limit' => 10]; $comment = TableRegistry::get('Comments'); $comments = $comment ->find() ->where(['Comments.post_id' => $id])
$this->paginate = ['limit' => 10];
$comment = TableRegistry::get('Comments');
$comments = $comment
->find()
->where(['Comments.post_id' => $id])
->contain(['Users'])
->order(['Comments.created' => 'ASC'])
->all()
->toArray(); //the results will be array
$comments = $this->paginate($comments);
谢谢大家! 内置分页器不支持数组,它只支持查询。如果需要对数组进行分页,则必须构建自定义分页器 但是,首先不需要传递数组,而是传递查询,如果确实需要,则将分页器返回的结果集(
\Cake\ORM\ResultSet |\Cake\Datasource\resultsterface
)转换为数组,结果相同,即:
$comments=$comment
->查找()
->其中(['Comments.post_id'=>$id])
->包含(['Users']))
->订单(['Comments.created'=>'ASC']);
$comments=$this->paginate($comments);
$comments=$comments->toArray();
我不知道你为什么要这样做,因为结果集实现了\ArrayAccess
,也就是说它可以像数组一样被迭代和访问,你只是在通过这种转换来限制自己
此外,没有真正直接的方法来实现自动化,唯一接近的方法是使用模型的来修改查询,并附加一个将结果集转换为数组的函数,但结果格式化程序可能会返回\Cake\Collection\CollectionInterface
,因为他们在某种程度上被承诺也会收到。格式化程序按顺序处理,后续格式化程序将接收前一个格式化程序返回的值