检索按剧院分组的电影放映时间:电影 - > Showtimes< -Theater in CakePHP

时间:2011-07-07 07:34:41

标签: mysql cakephp associations cakephp-1.3

当您观看电影时,我想要显示由剧院组织的放映时间(movie_times表)。我可以很好地获得所有数据,但它会像每个MovieTime一样返回Theatre数据:

[0] =>
    [Movie] =>
        [name] => Herp de Derp
    [MovieTime] =>
        [0] =>
            [datetime] => 2011-07-06 18:30:00
            [Theater] =>
                [name] => Awesome Cinemas
                [address] => 1234 Street Ln
        [1] =>
            [datetime] => 2011-07-06 20:30:00
            [Theater] =>
                [name] => Awesome Cinemas
                [address] => 1234 Street Ln
        [2] =>
            [datetime] => 2011-07-06 20:30:00
            [Theater] =>
                [name] => Crappy Movies 10
                [address] => 678 Avenue St

你可以想象,剧院数据有一些重复。

有没有办法可以获得相同的数据,而是将剧院数据与其中的movie_times一起分组?:

[0] =>
    [Movie] =>
        [name] => Herp de Derp
    [Theater] =>
        [0] =>
            [name] => Awesome Cinemas
            [address] => 1234 Street Ln
            [MovieTime] =>
                [0] =>
                    [datetime] => 2011-07-06 18:30:00
                [1] =>
                    [datetime] => 2011-07-06 20:30:00
                [2] =>
                    [datetime] => 2011-07-06 22:30:00

        [1] =>
            [name] => Awesome Cinemas
            [address] => 1234 Street Ln

我的协会如下:

Movie hasMany MovieTime
MovieTime belongsTo Movie

Theater hasMany MovieTime
MovieTime belongsTo Theater

正如您所看到的,电影和剧院之间没有直接联系 - 它使用MovieTime作为中间人。这些关联在逻辑上是合乎逻辑的(至少对我来说) - 但是如果我必须将它们改为不同的(但也是逻辑的)设置,我当然愿意听到它。

我正在使用它来获取我的数据:

        $data = $this->find("first", array(
            'conditions' => array(
                array('Movie.slug' => $slug)
            ),
            'fields' => array('id', 'name', 'slug', 'url', 'runtime'),
            'contain' => array(
                'MovieTime' => array(
                    'fields' => array('datetime', 'screen_number'),
                    'Theater' => array(
                        'fields' => array('id', 'slug', 'name', 'address', 'phone'),
                        'City' => array(
                            'fields' => array('id', 'name', 'st')
                        ),
                    ),
                ),
            ),
        )
    );

1 个答案:

答案 0 :(得分:0)

尝试GROUP BY。你只需要添加'group'=> '把这个de Model.Id'放到你的发现中。

试试这个:

$data = $this->find("first", array(
            'conditions' => array(
                array('Movie.slug' => $slug)
            ),
            'fields' => array('id', 'name', 'slug', 'url', 'runtime'),
            'contain' => array(
                'MovieTime' => array(
                    'fields' => array('datetime', 'screen_number'),
                    'group' => 'Theater.id',
                    'Theater' => array(
                        'fields' => array('id', 'slug', 'name', 'address', 'phone'),
                    'City' => array(
                        'fields' => array('id', 'name', 'st')
                    ),
                    ),
                ),
            ),
        ),
    );

有关详细信息,请查看以下两个链接: