配置:: read()的条件

时间:2013-02-03 14:13:43

标签: cakephp controller

我想选择帖子必须在我的“热门帖子”小部件上观看多少次。要做到这一点,请使用我的控制器

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

它完美无缺。 现在我用魔杖改变我的号码(100)

Configure::read('top_views');

但我不知道怎么办:/有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分: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控制器中,根据您自己的应用需要进行调整。通常这种事情应该在模型中。