CakePHP 2.1为habtm分页

时间:2013-01-17 13:05:45

标签: cakephp pagination cakephp-2.1 has-and-belongs-to-many paginate

我正在使用cakephp 2.1并开发一个包含以下表结构的应用程序。

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

和'人'habtm电影按表格如下。

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)

我想为特定的人拍摄电影并为他分享电影。 所以我编写的代码在控制器中

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

,结果如下

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

但是我想要获取限制为20的特定电影的所有电影。这样我就可以轻松地分页每页的电影限制..请帮助我获得解决方案..工作更受赞赏。< / p>

1 个答案:

答案 0 :(得分:1)

我建议更改您的数据模型,这样您只有一个HABTM表:

people_movies(id, person_id, movie_id,role_id) 

您还需要一个角色表:

role(id, role_name)

然后,您可以将它们全部存储在同一个表中,并在将来添加其他角色(如果需要)。

使用这种方法,查询以获取一个人的所有电影将更加简单。