我想选择一个帖子必须有多少浏览量在我的“热门帖子”小工具上。为了做到这一点,在我的控制器中,我
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');但是我不知道该怎么办:/有没有人能帮我一下?
发布于 2013-02-03 22:31:50
更改您的menu方法以获取具有默认值的参数,更新conditions数组。
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控制器中,根据您自己的应用程序的需要进行调整。通常情况下,这类东西应该在模型中。
https://stackoverflow.com/questions/14673384
复制相似问题