我是Cakephp的新手,我的分页不起作用..
我的控制器代码:
$this->paginate = array('Favourite' =>array('limit' => '1','conditions'=>array('user_id'=>$id)));
$cat_data = $this->paginate('Favourite');
$this->set('fav_data',$cat_data);
我的查看代码:
<!---Code for pagination -->
<div>
<?php echo $this->Paginator->prev('', null, null, array('class' => 'pdisable','escape' => false)); ?>
<div class="numbers"><?php echo $this->Paginator->numbers(); ?> Pages: <?php echo $this->Paginator->counter(); ?></div>
<?php echo $this->Paginator->next('', null, null, array('class' => 'ndisable','escape' => false)); ?>
</div>
它不会根据我给定的限制显示一个结果。它显示所有记录。
最佳答案
当您尝试使用代码中的以下行获取分页记录集时:$cat_data = $this->paginate('Favourite');
实际上,您实际上是在第二次重新声明分页设置,但不再指定任何限制或条件。您需要将Paginator
Component类与预先声明的Paginate设置一起使用。
在您的代码中:
//这里您一次声明分页设置。$this->paginate = array('Favourite' =>array('limit'=>'1','conditions'=>array('user_id'=>$id)));
//在这里您要重新声明它们,但不要指定限制或条件。这有效地覆盖了您所做的第一个设置声明。$cat_data = $this->paginate('Favourite');
$this->set('fav_data',$cat_data);
更改:$cat_data = $this->paginate('Favourite');
至:$cat_data = $this->Paginator->paginate('Favourite');
因此,您的控制器代码应如下所示:
//declare settings without conditions.
$this->paginate = array(
'limit' => 1
);
//use above settings to pull the paginated results.
//be sure to add the conditions you had in the settings before to the 2nd parameter of the `paginate()` function.
$this->set('fav_data', $this->Paginator->paginate('Favourite', array('user_id' => $id)));