Sanganabasu
Sanganabasu

Reputation: 927

CakePHP 2.1 pagination for habtm

I am using cakephp 2.1 and developing an application, which contain the following table structures.

1. people(id, name)
2. movies(id, name)

and 'people' habtm movies by tables as follows.

1. castsmovie(id, person_id, movie_id)
2. directorsmovie(id, person_id, movie_id)
3. moviesmusicdirector(id, person_id, movie_id)
4. moviesproducers(id, person_id, movie_id)
5. movieswriter(id, person_id, movie_id)
6. cinematographersmovie(id, person_id, movie_id)

I wanna fetch the movies for perticular person and paginate the movies for him. So the code I have written is in controller

$this->paginate = array('Person' => array(
        'contain' => array(
                'CastsMovie' => array('Movie'),
                'DirectorsMovie' => array('Movie'),
                'MoviesProducer' => array('Movie'),
                'MoviesMusicDirector' => array('Movie')),
                'MoviesWriter' => array('Movie'),
                'CinematographersMovie' => array('Movie')
        ),
        'conditions' => array('Person.id' => $id)));

and the result is as follows

    array(
(int) 0 => array(
    'Person' => array(
        'id' => '101',
        'name' => 'Darshan Thoogudeep',
    ),
    'CastsMovie' => array(
        (int) 0 => array(
            'id' => '53',
            'movie_id' => '14',
            'person_id' => '101',
            'order' => '1',
            'Movie' => array(
                'id' => '14',
                'name' => 'Krantiveera Sangolli Rayanna',
            )
        ),
        (int) 1 => array(
            'id' => '78',
            'movie_id' => '19',
            'person_id' => '101',
            'order' => '0',
            'Movie' => array(
                'id' => '19',
                'name' => 'Saarathi',
            )
        ),
        (int) 2 => array(
            'id' => '137',
            'movie_id' => '35',
            'person_id' => '101',
            'order' => '1',
            'Movie' => array(
                'id' => '35',
                'name' => 'BulBul',
            )
        ),
        (int) 3 => array(
            'id' => '153',
            'movie_id' => '42',
            'person_id' => '101',
            'order' => '0',
            'Movie' => array(
                'id' => '42',
                'name' => 'Viraat',
            )
        )
    ),
    'CinematographersMovie' => array(),
    'DirectorsMovie' => array(),
    'MoviesMusicDirector' => array(),
    'MoviesWriter' => array(),
    'MoviesProducer' => array()
)
 )

But I want to fetch all movies of perticular person with limit as 20.. So that I can easily paginate the limit of movies in each page.. Please help me to get the soluting.. The work is more appreciated.

Upvotes: 0

Views: 490

Answers (1)

cowls
cowls

Reputation: 24334

I would suggest changing your data model so you have just one HABTM table:

people_movies(id, person_id, movie_id,role_id) 

You would also need a role table:

role(id, role_name)

Then you can store them all in the same table and add additional roles in future if required.

Using this approach the query to get all movies for one person will be much simpler.

Upvotes: 1

Related Questions