2013-02-03 39 views
0

我想選擇一個帖子必須在我的「頂端帖子」小部件必須有多少意見。要做到這一點,在我的控制器我條件與配置::讀取()

public function menu() { 
    return $this->Post->find('all', array(
       'limit' => 5, 
       'order' => array('Post.id' => 'desc'), 
       'conditions' => array('Post.hits >= 100' 
       ))); 
} 

它的作品完美。 現在我的魔杖由

Configure::read('top_views'); 

改變我的電話號碼(100),但我不知道該怎麼做:/有沒有誰可以幫我這個?

回答

0

更改您的menu方法以採用默認值的參數更新條件數組。

public function menu($hits = 100) { 
    return $this->Post->find('all', array(
       'limit' => 5, 
       'order' => array('Post.id' => 'desc'), 
       'conditions' => array('Post.hits >=' => $hits) // here 
       )); 
} 

,那麼你可以調用它,並通過在任何你喜歡的值:

$this->set('top_posts', $this->Post->menu(Configure::read('top_views')); 
$more_than_500_hits = $this->Post->menu(500); 

這裏假設你的menu方法是在你的Post控制器,根據需要進行調整了自己的應用程序。通常這種事情應該在模型中。

+0

非常感謝,它的作品非常完美。 – AlcoolGeek