What is the difference between JOIN ON and JOIN WITH in Doctrine2?
Asked Answered
N

1

15

What is the difference between JOIN ON and JOIN WITH in Doctrine2?

I couldn't find any relevant info in the manual.

Neall answered 30/10, 2012 at 11:20 Comment(0)
P
19

ON replaces the original join condition,
WITH adds a condition to it.


Example:

[Album] ---OneToMany---> [Track]
  1. Case One

    DQL

    FROM Album a LEFT JOIN a.Track t WITH t.status = 1
    

    Will translate in SQL

    FROM Album a LEFT JOIN Track t ON t.album_id = a.id AND t.status = 1
    
  2. Case Two

    DQL

    FROM Album a LEFT JOIN a.Track t ON t.status = 1
    

    Will translate in SQL

    FROM Album a LEFT JOIN Track t ON t.status = 1
    
Pomegranate answered 30/10, 2012 at 11:23 Comment(3)
Thanks for your answer, but I don't really get your point, can you provide me an example?Neall
Yes, let's say you have an Album table linked with a Track table (one to many)... In DQL, if you do FROM Album a LEFT JOIN a.Track t WITH t.status = 1, it's the equivalent of SQL FROM Album a LEFT JOIN Track t ON t.album_id = a.id AND t.status = 1 But if you do FROM Album a LEFT JOIN a.Track t ON t.status = 1, it will be the equivalent of SQL FROM Album a LEFT JOIN Track t ON t.status = 1, thus replacing the relation condition (t.album_id = a.id). Hope I'm clear !Pomegranate
I'm not editing the answer because I'm not 100% sure I got it, but I reckon that you use Join::ON to set your own join conditions for this query and you use Join::WITH to use the conditions set in the entity model as starting point and merely add additional clauses. And, at least for me, Join::ON refuses to work in Symfony no matter what.Niple

© 2022 - 2024 — McMap. All rights reserved.