开发者

Retrieving a Movie's Showtimes grouped by Theater: Movie->Showtimes<-Theater in CakePHP

When you view a movie, I want to show the showtimes (movie_times tabl开发者_开发技巧e) organized by Theater. I can get all the data just fine, but it's returning the Theater data with EVERY MovieTime like this:

[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

As you can imagine, there is a TON of duplication with the theater data.

Is there a way I can get this same data, but instead, get the Theater data grouped with the movie_times within it like this?:

[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

My associations are the following:

Movie hasMany MovieTime
MovieTime belongsTo Movie

Theater hasMany MovieTime
MovieTime belongsTo Theater

As you can see, there's no direct link between Movie and Theater - it's using the MovieTime as a go-between. The associations make logical sense (to me at least) - but if I have to change them to a different (but also logical) setup, I'm certainly willing to hear it.

I'm using this to get my data currently:

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


Try the GROUP BY. You just need to add 'group' => 'Put here de Model.Id' into your find.

Try this:

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

For more information, check this two links:

  • http://debuggable.com/posts/how-to-group-by-in-cakephps-new-release-part-2:4850d2c9-edbc-4f0e-9e0e-07d64834cda3

  • http://nuts-and-bolts-of-cakephp.com/2008/06/03/new-way-to-group-by-in-cakephp

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜