Search code examples
cakephppaginationcakephp-2.1sql-limit

CakePHP 2.1 Custom Pagination


I am using cakephp 2.1 and defined 3 tables as follows.

   `industries(id, name);`
   `movies(id, name, industry_id),` 
   `trailers(id, name, movie_id);`

I want to paginate the trailers for particular industry. So the code I have written is below:

  $this->paginate = array(
                    'Industry' => array(
                        'contain' => array(
                            'Movie' => array(
                               'order' => 'Movie.release DESC', 
                               'Trailer' => array(
                                  'conditions' => array(
                                       'Trailer.id !=' => $trailer_id
                                   )
                                )
                             )
                         ), 
                         'conditions' => array(
                             'Industry.id' => $id
                         )
                      )
                  );

If I would specify 'limit' for 'Trailer', I get correct number of results but some null array for a movie which doesn't contain any trailers. Please help me to get the number of specified limit of trailers for particular industry. The work will be most appreciated.


Solution

  • It looks to me like you've got your contain key in the wrong place. It should be defined before the models.

    <?php
    $this->paginate = array(
      'contain' => array(
        'Industry' => array(
          'Movie' => array(
            'order' => 'Movie.release DESC', 
            'Trailer' => array(
              'conditions' => array(
                'Trailer.id !=' => $trailer_id
              )
            )
          )
        )
      ),
        'conditions' => array(
          'Industry.id' => $id
      )
    );