introduction.txt 1.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
Doctrine Query Language (DQL) is an Object Query Language created for helping users in complex object retrieval. You should always consider using DQL (or raw SQL) when retrieving relational data efficiently (eg. when fetching users and their phonenumbers).

When compared to using raw SQL, DQL has several benefits: 
    
* From the start it has been designed to retrieve records(objects) not result set rows
* DQL understands relations so you don't have to type manually sql joins and join conditions
* DQL is portable on different databases
* DQL has some very complex built-in algorithms like (the record limit algorithm) which can help developer to efficiently retrieve objects
* It supports some functions that can save time when dealing with one-to-many, many-to-many relational data with conditional fetching.

If the power of DQL isn't enough, you should consider using the rawSql API for object population.

zYne's avatar
zYne committed
13
You may already be familiar with the following syntax:
14
<code type="php">
zYne's avatar
zYne committed
15 16
// DO NOT USE THE FOLLOWING CODE
// (uses many sql queries for object population)
17 18 19 20

$users = $conn->getTable('User')->findAll();

foreach($users as $user) {
zYne's avatar
zYne committed
21 22
    print $user->name . ' has phonenumbers: ';

23
    foreach($user->Phonenumber as $phonenumber) {
zYne's avatar
zYne committed
24
        print $phonenumber . ' ';
25 26
    }
}
27
</code>
zYne's avatar
zYne committed
28

29
However you should not use it. Below is the same behaviour implemented much more efficiently:
30

zYne's avatar
zYne committed
31 32
<code type="php">
// same thing implemented much more efficiently:
33 34
// (using only one sql query for object population)

zYne's avatar
zYne committed
35
$users = $conn->query('FROM User u LEFT JOIN u.Phonenumber p');
36 37

foreach($users as $user) {
zYne's avatar
zYne committed
38 39
    print $user->name . ' has phonenumbers: ';

40
    foreach($user->Phonenumber as $phonenumber) {
zYne's avatar
zYne committed
41
        print $phonenumber . ' ';
42 43 44
    }
}
</code>
zYne's avatar
zYne committed
45