douqiu0351 2017-02-19 17:01
浏览 65

CakePHP 3:如何使用自定义分页?

I don't succeed in using the pagination with a custom query. I follow exactly what's in the doc, but it doesn't work. The point is to paginate a list of records, having the flag 'valid' to 'Y' or 'N'.

In the controller:

if (!isset($this->request->query['valid']) || $this->request->query['valid'] == '')
  $allFilters['valid'] = 'N';
else
  $allFilters['valid'] = 'Y';
$this->paginate = ['finder' => ['curnames' => $allFilters]];
$data = $this->paginate($this->Names)->toArray();

In the model:

public $paginate = ['finder' => 'curnames', 'limit' => 25, 'order' => ['Names.id' => 'asc']];

public function findCurnames(Query $query, array $options) {
  $query->where([
    'valid' => $option['valid']
  ]);
  return $query;
}  

When I execute the code, I get a Cake\Network\Exception\NotFoundException exception. What I am missing?

Update: The version is 3.3. The error is triggered when this is executed:

$data = $this->paginate($this->Names)->toArray();

Update : In the controller, I've change the line

$this->paginate = ['finder' => ['curnames' => $allFilters]];

to

$paginate = ['finder' => ['curnames' => $allFilters]];

and the error doesn't pop up anymore. But the condition filtering on the 'valid'='Y' or 'N' is not taken into account. So it still doesn't work.

  • 写回答

2条回答 默认 最新

  • dousu8767 2017-02-20 10:38
    关注

    I got the answer. The error comes from a typo. Correct code should be

    'valid' => $options['valid']
    

    Options with a 's'. Now it works.

    评论

报告相同问题?

悬赏问题

  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM