2013-08-26 53 views
1

我剛剛烘焙了一個簡單的CakePHP應用程序,並試圖自定義記錄的分頁方式。我在我的控制器中有這個動作:

public function index() { 
    $this->Recipe->recursive = 0; 
    $this->set('recipes', $this->Recipe->paginate()); 
} 

這對默認分頁正常工作。我想定製量的行返回,並通過使用一個類屬性的順序在同一個控制器稱爲$paginate

public $paginate = array(
    'limit' => 1, 
    'order' => array(
     'Recipe.title' => 'asc' 
    ) 
); 

但是它採取沒有任何影響。結果仍然有默認限制和排序順序。我也試過在我的行動建立$this->paginate,然而,這似乎變得也被忽略:

public function index() { 
    $this->paginate = array(
     'limit' => 1, 
     'order' => array(
      'Recipe.title' => 'asc' 
     ) 
    ); 
    $this->set('recipes', $this->Paginator->paginate()); 
} 

什麼引起蛋糕不理我設置分頁選項?當你烘烤我不知道的應用程序時,它可能會做些什麼?

+0

什麼是你確切的CakePHP的版本? – burzum

回答

2

嘗試

public function index() { 
    $this->Paginator->settings = array(
     'limit' => 1, 
     'order' => array(
      'Recipe.title' => 'asc' 
     ) 
    ); 
    $this->set('recipes', $this->Paginator->paginate()); 
} 
+0

這工作,雖然我不明白爲什麼。有沒有什麼辦法可以按照CakePHP文檔中提到的方式進行,並且爲我的整個控制器設置了全局分頁設置? –

+0

$ this-> Paginator-> settings在您的控制器的beforeFilter()中設置您的默認值。 $ this-> paginate仍然適用於向後兼容的原因。你的確切的CakePHP版本是什麼?是的,看起來手冊不是最新的。 – burzum