2012-01-08 27 views
1

我有两个控制器我应该在这种情况下使用更多的查找吗?

Sections_controller.php 
Articles_controller.php 

Section model hasmany Article... 

我想获取文章像所有新闻sites..every块块的形式有链接到本section..so我用这个代码中的物品节的名称。 ..... 第一块

$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)

回答

1

任何查找(S)应在模型中进行,而不是控制器 - 这个遵循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); 

我敢肯定有东西,可以做到这一点,使这更多精益......等,但它我喜欢如何编写它,以便于使用和易读,并且至少为您开始思考如何考虑模型方法,以及为不同目的使用和重用它们的能力。

+0

谢谢..我喜欢第一个代码块..但还有一个问题关于“胖模型,皮包骨头控制器”的原理..如果我遵循这个原则,它会运行我的脚本快速或降低带宽?..谢谢 – user1080247 2012-01-09 04:27:48

+0

我相信它更多的是关于速度或带宽的代码组织。 – Dave 2012-01-09 13:25:33

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); 
相关问题