2015-01-14 24 views
3

我試圖做CakePHP的定製進行分頁設置的條件,事情是,我想通過自定義的條件,這樣的事情:爲分頁

$conditions['People'] = $this->People->find('all', 
    array(
     'order' => array('People.id DESC'), 
     'limit' => 16, 
     'order' => 'People.id DESC' 
    ) 
); 

$people = $this->paginate('People',$conditions); 

這基本上是所有涉及分頁的代碼,但它它不會工作,它會引起它找不到列人...

我只想做自定義條件的分頁,也就是說,我想事先設置條件,有沒有辦法做那?

回答

3

好吧,我解決了這個問題,這就是我所做的: 首先我聲明它可以是空的,如果你想:

public $paginate = array('People'=>array(
     'limit' => 6, 
     'order' => 'People.id DESC' 
    )); 

然後,只要我想改變一些東西,我只是做:

$this->paginate['People']['limit'] = 12; 
+0

好吧。很高興聽到你有它的工作。 –

4

你看過文檔嗎?

據的文檔,你應該添加的條件是這樣的:

public function list_recipes() { 
    $this->Paginator->settings = array(
     'conditions' => array('Recipe.title LIKE' => 'a%'), 
     'limit' => 10 
    ); 
    $data = $this->Paginator->paginate('Recipe'); 
    $this->set(compact('data')); 
} 

因此猜測這應該工作:

public function list_people() { 
    $this->Paginator->settings = array('order' => array('People.id DESC'),'limit' => 16,'order' => 'People.id DESC'); 
    $data = $this->Paginator->paginate('People'); 
    $this->set(compact('data')); 
} 
+0

好吧,我這樣做,但現在我得到「超載財產天道酬勤的間接修改oController :: $ Paginator沒有效果「 我試圖用文檔解決它,做」$ this-> Paginator = array();「但沒有... – WhiteFloater

+1

添加Paginator組件並閱讀文檔。 – burzum

4

這裏是我如何在蛋糕2.X做

首先是一定要包括在控制器中分頁程序組件和幫手,像

$public $components = array(
    'Paginator' 
); 

$public $helpers = array(
    'Paginator' 
); 

然後在動作

$options = array(
    'conditions' => array(
     'Post.status' => 1 
    ), 
    'fields' => array(
     'Post.id', 
     'Post.title', 
     'Post.created' 
    ), 
    'order' => array(
     'Post.created' => 'DESC' 
    ), 
    'limit' => 10 
); 

$this->Paginator->settings = $options; 
$posts = $this->Paginator->paginate('Post');