當您查看電影時,我想要顯示劇院組織的放映時間(電影時間表)。我可以得到所有的數據只是罰款,但它與每一個MovieTime這樣返回劇場數據:檢索按劇院分組的電影放映時間:電影 - >放映時間< - CakePHP中的劇院
[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')
),
),
),
),
)
);
當我這樣做,我得到:U在'字段列表'中已知列'Theater.id' – Dave
劇院是我的模型名稱,id是主鍵。我不想通過MovieTime.id進行分組 - 這與我想要的相反。 – Dave
我改變了我的第一個答案,試試。 –