Symfony和Doctrine选择查询无法正常工作

时间:2011-12-27 10:02:26

标签: php mysql symfony1 doctrine

我在视频和播放列表中有M:M关系(playlist_has_video)

例如,如果我在播放列表中添加相同的视频3次,当我尝试从该播放列表中选择视频时,我只获得一个结果(视频),但我需要获得3个结果(视频)。

我的查询是:

$q_videos = Doctrine_Query::create()
            ->select('V.*, PHV.*')
            ->distinct(FALSE)
            ->from('Video V')
            ->leftJoin('V.VideoSource VS')
            ->leftJoin('V.PlaylistHasVideo PHV')
            ->orderBy("PHV.position ASC")
            ->where('PHV.playlist_id = ? AND V.instance_id = ? AND VS.transcoded = ?', array($this->getObject()->getId(), sfContext::getInstance()->getUser()->getAttribute('instance_id'), 1));

我在日志中检查查询并确认:

SELECT v.id AS v__id, v.published AS v__published, v.share AS v__share, v.public AS v__public, v.title AS v__title, v.description AS v__description, v.thumbnail AS v__thumbnail, v.subtitle AS v__subtitle, v.created_at AS v__created_at, v.updated_at AS v__updated_at, v.views AS v__views, v.highlighted AS v__highlighted, v.created_by AS v__created_by, v.updated_by AS v__updated_by, v.instance_id AS v__instance_id, p.id AS p__id, p.playlist_id AS p__playlist_id, p.video_id AS p__video_id, p.position AS p__position FROM video v LEFT JOIN video_source v2 ON v.id = v2.video_id LEFT JOIN playlist_has_video p ON v.id = p.video_id WHERE (p.playlist_id = '1' AND v.instance_id = '1' AND v2.transcoded = '1') ORDER BY p.position ASC

在phpMyAdmin中执行此查询时,它会返回我的好结果 - 3个结果(视频)

学说版本:1.2.3 Symfony版本:1.4.8

任何人都可以帮助我?

1 个答案:

答案 0 :(得分:1)

问题是您在leftJoin上使用Video。您可以添加->groupBy('PHV.playlist_id')并设置->from('PlaylistHasVideo PHV')

像phpMyAdmin这样的工具不像Doctrine那样做水合作用,所以你会看到另一个结果,而不是Doctrine给你的。