DQL join between unrelated entities?

2019-06-17 23:14发布

Can I have a DQL join between unrelated entities using WITH DQL operator? OR is definign relationship absolutely mandatory?

I have a unidirectional relationship with Category and CategorySubsription. Where CategorySubscription has a many to one unidirectional relationship with Category. I want to grab a list of Categories c and left join CategorySubscription cs WITH cs.category_id = c.id AND cs.user_id = value.

Can I do this somehow?

2条回答
孤傲高冷的网名
2楼-- · 2019-06-17 23:42

You can try using multiple from() methods and join conditions put in where() or andWhere methods()

查看更多
祖国的老花朵
3楼-- · 2019-06-17 23:54

Starting with Doctrine version 2.3 you can, as mentioned in a blog.

It's also mentioned in the docs if you know where to look. Scroll down to the last example under "15.2.4. DQL SELECT Examples":

Joins between entities without associations were not possible until version 2.4, where you can generate an arbitrary join with the following syntax:

<?php
$query = $em->createQuery('SELECT u FROM User u JOIN Blacklist b WITH u.email = b.email');

I know it says "not possible until version 2.4", but it definitely works starting with 2.3!

查看更多
登录 后发表回答