我想在一個分頁查詢中使用中容納。代碼如下,完整。CakePHP的中容納和PAGINATE - 深協會沒有出現
$this->paginate = array(
'conditions'=>array(
'Track.pending'=>0,
'Track.status'=>1
),
'contain'=>array(
'Vote'=>array(
'conditions'=>array(
'Vote.created >'=>$start,
'Vote.created <='=>$end
),
'fields'=>array(
'Vote.vote_type_id',
'Vote.id',
),
),
'Artist'=>array(
'Image'=>array(
'fields'=>array(
'Image.name'
)
),
'Genre'=>array(
'fields'=>array(
'Genre.name'
)
),
'fields'=>array(
'Artist.name',
'Artist.id',
'Artist.slug',
'Artist.city',
'Artist.genre_id'
)
)
),
'fields'=>array(
'Track.id',
'Track.slug',
'Track.name',
'Track.featured',
'Track.plays',
'Track.vote_score',
'Track.vote_week_score',
'Track.video',
'Track.status',
'Track.created'
),
'order'=>$order
);
直接關聯(投票和藝術家)被選中,但圖像和流派不是。這裏是正在生成的SQL:
SELECT COUNT(*) AS `count` FROM `tracks` AS `Track` LEFT JOIN `artists` AS `Artist` ON (`Track`.`artist_id` = `Artist`.`id`) WHERE `Track`.`pending` = 0 AND `Track`.`status` = 1
SELECT `Track`.`id`, `Track`.`slug`, `Track`.`name`, `Track`.`featured`, `Track`.`plays`, `Track`.`vote_score`, `Track`.`vote_week_score`, `Track`.`video`, `Track`.`status`, `Track`.`created`, `Artist`.`name`, `Artist`.`id`, `Artist`.`slug`, `Artist`.`city`, `Artist`.`genre_id` FROM `tracks` AS `Track` LEFT JOIN `artists` AS `Artist` ON (`Track`.`artist_id` = `Artist`.`id`) WHERE `Track`.`pending` = 0 AND `Track`.`status` = 1 ORDER BY `Track`.`vote_week_score` DESC LIMIT 20
SELECT `Vote`.`vote_type_id`, `Vote`.`id`, `Vote`.`track_id` FROM `votes` AS `Vote` WHERE `Vote`.`created` > '2011-09-26' AND `Vote`.`created` <= '2011-10-03' AND `Vote`.`track_id` IN (24, 35, 31, 25, 27, 34, 56, 58)
蛋糕沒有拿起更深的關聯。這是Containable + paginate的限制還是我缺少一些東西?
謝謝!
你試過獲取數據,而PAGINATE?我的猜測是,你也不會在那裏得到它。我不確定這是否有幫助,但我經常發現JOIN的工作比遏制更好 - 不知道爲什麼。 – Dave
@戴夫,謝謝你,是的,我有代碼的其他地方說出來得到了相同的數據,而無需分頁,和它的作品,我想它。 – Will