Cakephp分页不起作用?

时间:2014-09-06 05:09:20

标签: mysql cakephp

我是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(); ?>&nbsp;&nbsp;Pages: <?php echo $this->Paginator->counter(); ?></div>
<?php echo $this->Paginator->next('', null, null, array('class' => 'ndisable','escape' => false)); ?>
 </div>

根据我给出的限制,它没有显示一个结果。它显示所有记录。

1 个答案:

答案 0 :(得分:1)

当您尝试在代码中使用此行获取分页记录集时:

$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)));