0
votes

I have M:M relation between video and playlist table (playlist_has_video)

For example if I add the same video 3 times in playlist and when i try to select videos from that playlist I get only one result (video), but I need to get 3 results (videos).

My query is:

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

I check query in log and its OK:

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

When execute this query in phpMyAdmin it return me good results - 3 results (videos)

Doctrine Version: 1.2.3 Symfony Version: 1.4.8

Anyone can help me?

1

1 Answers

1
votes

The problem is that you are using leftJoin on Video. You may add ->groupBy('PHV.playlist_id') and set ->from('PlaylistHasVideo PHV').

Tools like phpMyAdmin don't do hydration like Doctrine does, so you see another results than Doctrine gives to you.