在Cakephp中使用Paginator时出错

时间:2016-06-16 13:12:58

标签: cakephp-3.0 paginator

我有一个用于分页数据的变量$ paginator

在我的控制器中

public $paginator = [
    'limit'=>5,
    'order'=>[
        'author'=>'desc'
    ]
];

在我的视图模板(.ctp)

<?= $this->Paginator->prev('<< pre') ?>
<?= $this->Paginator->next('next >>')?>
<?= $this->Paginator->counter() ?>

但我收到很多错误:

  

注意(8):未定义索引:pageCount [CORE / src / View / Helper / PaginatorHelper.php,第591行]   注意(8):未定义索引:count [CORE / src / View / Helper / PaginatorHelper.php,第595行]   注意(8):未定义索引:perPage [CORE / src / View / Helper / PaginatorHelper.php,第598行]   注意(8):未定义索引:count [CORE / src / View / Helper / PaginatorHelper.php,第599行]   注意(8):未定义索引:count [CORE / src / View / Helper / PaginatorHelper.php,第600行]   注意(8):未定义索引:页面[CORE / src / View / Helper / PaginatorHelper.php,第613行]   注意(8):未定义索引:当前[CORE / src / View / Helper / PaginatorHelper.php,第615行]   注意(8):未定义索引:count [CORE / src / View / Helper / PaginatorHelper.php,第616行]

我认识到只有这一行导致错误

<?= $this->Paginator->counter() ?>

我无法分页。 为什么会这样? :(

1 个答案:

答案 0 :(得分:0)

在控制器分页属性中

public $paginate = [
        'States' => ['scope' => 'states'],
        'Lgas' => ['scope' => 'lgas'],
];

在控制器动作中

  $lgas = $this->paginate($this->Lgas, ['scope' => 'lgas']);
  $states = $this->paginate($this->States, ['scope' => 'states']);
  $this->set(compact('lgas', 'states'));

在视图中

<div class="paginator">
        <ul class="pagination">
            <?=$this->Paginator->first('<< ' . __('first'), ['model' => 'Lgas', 'tab' => 'lga'])?>
            <?=$this->Paginator->prev('< ' . __('previous'), ['model' => 'Lgas', 'tab' => 'lga'])?>
            <?=$this->Paginator->numbers(['model' => 'Lgas', 'tab' => 'lga'])?>
            <?=$this->Paginator->next(__('next') . ' >', ['model' => 'Lgas', 'tab' => 'lga'])?>
            <?=$this->Paginator->last(__('last') . ' >>', ['model' => 'Lgas', 'tab' => 'lga'])?>
        </ul>
        <p><?=$this->Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total'), 'model' => 'Lgas', 'tab' => 'lga'])?></p>
    </div>

    <div class="paginator">
        <ul class="pagination">
            <?=$this->Paginator->first('<< ' . __('first'), ['model' => 'States'])?>
            <?=$this->Paginator->prev('< ' . __('previous'), ['model' => 'States'])?>
            <?=$this->Paginator->numbers(['model' => 'States'])?>
            <?=$this->Paginator->next(__('next') . ' >', ['model' => 'States'])?>
            <?=$this->Paginator->last(__('last') . ' >>', ['model' => 'States'])?>
        </ul>
        <p><?=$this->Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total'), 'model' => 'States'])?></p>
    </div>