2012-08-11 115 views
0

我正在制作一个电影网站项目。我需要限制5个评论每个电影页面。如何限制cakephp中的递归

$this->Movie->find('first', array(
    'conditions' => array('Movie.url' => $req), // URL to fetch the required page 
    'recursive' => 1 
)); 

使用上面的代码我得到1电影的细节和所有(目前近20)与该电影有关的评论。那么我怎样才能将评论限制在5?

+0

ofcourse,我会接受,如果它适合我​​。 我根本无法接受那些不适合我的人。 – 2012-08-12 02:18:34

回答

0

我想没有人知道如何解决,我发现了一些我可以暂时解决的方法。 请有人告诉我最佳做法。

$movie = $this->Movie->find('first', array(
    'conditions' => array('Movie.url' => $req), 
'recursive' => 0 
)); 
$this->loadModel('MovieReview'); 
$movieReview = $this->MovieReview->find('all',array(
    'conditions' => array('MovieReview.movie_id' => $movie['Movie']['id']), 
    'limit' => 5, 
    'recursive' => -1 
)); 
$movie['MovieReview'] = $movieReview; 

如果有人需要回答这个问题,请回答最佳做法。

+0

没有最佳实践,因为无法限制仅运行一个查询的相关模型的结果。您必须运行第二个查询或运行复杂/原始SQL查询来完成此操作。 – rlcabral 2012-08-13 22:06:06

1

你可以试试这个:

$this->Movie->find('all', array(
    'conditions' => array('Movie.url' => $req), // URL to fetch the required page 
    'limit'=>5, 
    'recursive' => -1 
)); 
+0

不,这不起作用,因为我在电影模型中没有任何评论ID。请检查我自己的答案。我修好了它。但我需要最佳实践。 – 2012-08-12 10:44:26

0

对协会的尝试,设定限制选项

 /** 
    * hasMany associations 
    * 
    * @var array 
    */ 
    public $hasMany = array(
     'MovieReview' => array(
      'className' => 'MovieReview', 
      'foreignKey' => 'movie_id', 
      'dependent' => false, 
      'conditions' => '', 
      'fields' => '', 
      'order' => '', 
      'limit' => '25', 
      'offset' => '', 
      'exclusive' => '', 
      'finderQuery' => '', 
      'counterQuery' => '' 
     ) 
    ); 
1
$this->Movie->find('all', array(
    'conditions' => array('Movie.url' => $req), // URL to fetch the required page 
    'contain' => array('MovieReview' => array('limit' => 5)) 
)); 

有了这个,你只会得到5 MovieReview每部电影返回

对不起,我的英语

+1

即使这可能是正确的答案,请不要犹豫,添加一些文本来解释什么代码。 – AFract 2015-02-11 16:20:26