Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DQL join between unrelated entities?

Tags:

doctrine-orm

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?

like image 441
DavidW Avatar asked Jan 30 '12 07:01

DavidW


People also ask

Which JPQL join will eagerly load the related entities?

7. Fetch Joins. Now let's talk about fetch joins. Their primary usage is for fetching lazy-loaded associations eagerly for the current query.

How do you use join fetch in JPA?

The FETCH keyword of the JOIN FETCH statement is JPA-specific. It tells the persistence provider to not only join the 2 database tables within the query but to also initialize the association on the returned entity. You can use it with a JOIN and a LEFT JOIN statement.


1 Answers

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!

like image 67
Jasper N. Brouwer Avatar answered Oct 09 '22 09:10

Jasper N. Brouwer