1
0
mirror of synced 2024-12-15 15:46:02 +03:00
doctrine2/manual/docs/en/dql-doctrine-query-language/join-syntax.txt

78 lines
2.7 KiB
Plaintext
Raw Normal View History

2007-07-31 23:47:17 +04:00
Syntax:
<code>
2007-09-02 02:35:43 +04:00
[[LEFT | INNER] JOIN <component_reference1>] [ON | WITH] <join_condition1> [MAP] <map_condition1>,
[[LEFT | INNER] JOIN <component_reference2>] [ON | WITH] <join_condition2> [MAP] <map_condition2>,
...
[[LEFT | INNER] JOIN <component_referenceN>] [ON | WITH] <join_conditionN> [MAP] <map_conditionN>
2007-07-31 23:47:17 +04:00
</code>
DQL supports two kinds of joins INNER JOINs and LEFT JOINs. For each joined component, you can optionally specify an alias.
* The default join type is {{LEFT JOIN}}. This join can be indicated by the use of either {{LEFT JOIN}} clause or simply '{{,}}', hence the following queries are equal:
2007-09-06 20:31:07 +04:00
<code type="sql">
2007-07-31 23:47:17 +04:00
SELECT u.*, p.* FROM User u LEFT JOIN u.Phonenumber
SELECT u.*, p.* FROM User u, u.Phonenumber p
</code>
The recommended form is the first one.
* {{INNER JOIN}} produces an intersection between two specified components (that is, each and every record in the first component is joined to each and every record in the second component). So basically {{INNER JOIN}} can be used when you want to efficiently fetch for example all users which have one or more phonenumbers.
2007-09-06 20:31:07 +04:00
<code type="sql">
2007-07-31 23:47:17 +04:00
SELECT u.*, p.* FROM User u INNER JOIN u.Phonenumber p
</code>
By default DQL auto-adds the primary key join condition, so for DQL query:
2007-09-06 20:31:07 +04:00
<code type="sql">
2007-07-31 23:47:17 +04:00
SELECT u.id, p.id FROM User u LEFT JOIN u.Phonenumber
</code>
Would have a SQL equivalent:
2007-09-06 20:31:07 +04:00
<code type="sql">
2007-07-31 23:47:17 +04:00
SELECT u.id AS u__id, p.id AS p__id FROM User u LEFT JOIN Phonenumber p ON u.id = p.user_id
</code>
2007-09-02 02:35:43 +04:00
+++ ON keyword
2007-07-31 23:47:17 +04:00
If you want to override this behaviour and add your own custom join condition you can do it with the {{ON}} keyword. Consider the following DQL query:
2007-09-06 20:31:07 +04:00
<code type="sql">
2007-07-31 23:47:17 +04:00
SELECT u.id, p.id FROM User u LEFT JOIN u.Phonenumber ON u.id = 2
</code>
This query would be converted into SQL:
2007-09-06 20:31:07 +04:00
<code type="sql">
2007-07-31 23:47:17 +04:00
SELECT u.id AS u__id, p.id AS p__id FROM User u LEFT JOIN Phonenumber p ON u.id = 2
</code>
2007-09-02 02:35:43 +04:00
+++ WITH keyword
2007-07-31 23:47:17 +04:00
Most of the time you don't need to override the primary join condition, rather you may want to add some custom conditions. This can be achieved with the {{WITH}} keyword.
DQL:
2007-09-06 20:31:07 +04:00
<code type="sql">
2007-07-31 23:47:17 +04:00
SELECT u.id, p.id FROM User u LEFT JOIN u.Phonenumber WITH u.id = 2
</code>
SQL:
2007-09-06 20:31:07 +04:00
<code type="sql">
2007-07-31 23:47:17 +04:00
SELECT u.id AS u__id, p.id AS p__id FROM User u LEFT JOIN Phonenumber p ON u.id = p.user_id AND u.id = 2
</code>
2007-09-02 02:35:43 +04:00
2007-07-31 23:58:44 +04:00
The Doctrine_Query API offers two convenience methods for adding JOINS. These are called innerJoin() and leftJoin(), which usage should be quite intuitive as shown below:
<code type="php">
$q = new Doctrine_Query();
$q->from('User u')
->leftJoin('u.Group g')
->innerJoin('u.Phonenumber p WITH u.id > 3')
->leftJoin('u.Email e');
$users = $q->execute();
2007-09-02 02:35:43 +04:00
</code>