在这种情况下我应该使用更多吗?

时间:2012-01-08 00:15:22

标签: cakephp cakephp-1.3 cakephp-2.0

我有两个控制器

Sections_controller.php
Articles_controller.php

Section model hasmany Article...

我想以所有新闻网站的形式获取文章。每个块都有部分名称,链接到本节内的文章。所以我使用此代码...... 第一个区块

 $block1=$this->Article->find('all',
         array(
          'limit' => 4, // just fetch 4 articles
          'order' => array('Article.created'=>'DESC'), 
          'conditions' => array('Section_id' => 87)
            )
          );
            // set the section for the view
            $this->set(compact('block1'));

第二块

 $block2=$this->Article->find('all',
         array(
         'limit' => 4, // just fetch 4 articles
          'order' => array('Article.created'=>'DESC'),
        'conditions' => array('Section_id' => 88)
            )
          );
            // set the section for the view
            $this->set(compact('block2'));

等......

任何人都有这个任务中最好的方法,没有重复查找代码.. 通知 ..我无法在函数中传递$ id,因为文章必须在请求网站索引示例时显示(www.newssite.com)

2 个答案:

答案 0 :(得分:1)

任何查找应该在模型中完成,而不是控制器 - 这遵循MVC结构以及“胖模型,瘦调控制器”的口头禅,这有助于保持MVC理念。

这不仅是“应该”完成的方式,而且还允许您将代码放在一个地方:

//in the Article model
function getArticlesBySection($id) {
     $articles = $this->find('all', array(
         'limit' => 4,
         'order' => array('Article.created'=>'DESC'),
         'conditions' => array('Section_id' => $id)
     ));
    return $articles;
}

//in the Articles controller
$block1 = $this->Article->getArticlesBySection('87');
$block2 = $this->Article->getArticlesBySection('88');
$this->set(compact('block1', 'block2'));

以上内容应该可以正常运行,但是您可以做很多事情来改进它 - 比如通过接受它来设置它更加灵活选项数组:

//in the Article model
function getArticles($id, $opts = null) {
    $params = array();

    //limit
    $params['limit'] = 100; //catchall if no limit is passed
    if(!empty($opts['limit'])) $params['limit'] = $opts['limit'];

    //order
    $params['order'] = array('Article.created'=>'DESC');
    if(!empty($opts['order'])) $params['order'] = $opts['order'];

    //conditions
    $params['conditions'] = array();
    if(!empty($opts['sections'])) array_push($params['conditions'], array('Section_id'=>$opts['sections']));

    $articles = $this->find('all', $params);
    return $articles;
}

//in the Articles controller
$opts = array('limit'=>4, 'sections'=>array('87'));
$block1 = $this->Article->getArticles($opts);

$opts['sections'] = array('88');
$block2 = $this->Article->getArticles($opts);

我确信有些事情可以做到更加精益......等等,但我喜欢写它以方便使用和阅读,并且至少让你开始思考如何思考模型方法,以及为不同目的使用和重用它们的能力。

答案 1 :(得分:0)

您可以使用直接的mysql查询来完成此操作,但我不确定如何将其放入蛋糕model->find函数中。你可以这样做:

$articles = $this->Article->query("SELECT * FROM articles a WHERE (SELECT COUNT(*) FROM articles b WHERE a.Section_id = b.Section_id AND a.created < b.created) < 4 ORDER BY a.Section_id, a.created DESC");

/* if you look at the results, you should have the 4 latest articles per section. 
Now you can loop through and set up an array to filter them by section. Modify to fit your needs */

foreach($articles as $article) {
    $results[$article['Article']['Section_id']][] = $article;
}

$this->set('results',$results);