praveen kumar
praveen kumar

Reputation: 848

How to limit the recursive in cakephp

I'm working on a movie website project. I need to limit 5 reviews per movie page.

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

Using the above code I'm getting 1 movie details and all(almost 20 for now) the reviews related to that movie. So how can I limit the reviews to 5 ?

Upvotes: 0

Views: 7661

Answers (4)

Martin
Martin

Reputation: 1

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

With this, you'll get only 5 MovieReview for each Movie returned

Sorry for my english

Upvotes: 0

Palhais
Palhais

Reputation: 1

Try on associations , set limit option

     /**
     * 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' => ''
        )
    );

Upvotes: 0

Codegiant
Codegiant

Reputation: 2150

You can try this:

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

Upvotes: 1

praveen kumar
praveen kumar

Reputation: 848

I think no one knows how to solve, I found the some way that I can solve it temporarily. Please someone tell me about the best practice.

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

If someone needs to answer this, pls answer the best practice.

Upvotes: 1

Related Questions