You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 9, 2017. It is now read-only.
SELECT b.id AS b__id, b.title AS b__title, b.content AS b__content, b.created_at AS b__created_at, b.updated_at AS b__updated_at FROM blog_article b
316
+
314
317
>**SIDEBAR**
315
318
>Hydrating
316
319
>
317
320
>The call to `->execute()` is actually much more powerful than a simple SQL query. First, the SQL is optimized for the DBMS you choose. Second, any value passed to the `Doctrine_Query` is escaped before being integrated into the SQL code, which prevents SQL injection risks. Third, the method returns an array of objects, rather than a result set. The ORM automatically creates and populates objects based on the database result set. This process is called hydrating.
318
321
319
322
For a more complex object selection, you need an equivalent of the WHERE, ORDER BY, GROUP BY, and other SQL statements. The `Doctrine_Query` object has methods and parameters for all these conditions. For example, to get all comments written by Steve, ordered by date, build a `Doctrine_Query` as shown in Listing 8-13.
320
323
321
-
Listing 8-13 - Retrieving Records by Criteria with `doSelect()`--Criteria with Conditions
324
+
Listing 8-13 - Retrieving Records by a `Doctrine_Query`with `createQuery()`--Doctrine_Query with Conditions
322
325
323
326
[php]
324
327
$q = Doctrine_Core::getTable('Comment')
@@ -327,6 +330,9 @@ Listing 8-13 - Retrieving Records by Criteria with `doSelect()`--Criteria with C
327
330
->orderBy('c.created_at ASC');
328
331
$comments = $q->execute();
329
332
333
+
// Will result in the following SQL query
334
+
SELECT b.id AS b__id, b.article_id AS b__article_id, b.author AS b__author, b.content AS b__content, b.created_at AS b__created_at, b.updated_at AS b__updated_at FROM blog_comment b WHERE (b.author = ?) ORDER BY b.created_at ASC
335
+
330
336
Table 8-1 compares the SQL syntax with the `Doctrine_Query` object syntax.
331
337
332
338
Table 8-1 - SQL and Criteria Object Syntax
@@ -355,6 +361,9 @@ Listing 8-14 - Another Example of Retrieving Records by Doctrine_Query with `cre
355
361
->orderBy('c.created_at ASC');
356
362
$comments = $q->execute();
357
363
364
+
// Will result in the following SQL query
365
+
SELECT b.id AS b__id, b.article_id AS b__article_id, b.author AS b__author, b.content AS b__content, b.created_at AS b__created_at, b.updated_at AS b__updated_at, b2.id AS b2__id, b2.title AS b2__title, b2.content AS b2__content, b2.created_at AS b2__created_at, b2.updated_at AS b2__updated_at FROM blog_comment b LEFT JOIN blog_article b2 ON b.article_id = b2.id WHERE (b.author = ? AND b2.content LIKE ?) ORDER BY b.created_at ASC
366
+
358
367
Just as SQL is a simple language that allows you to build very complex queries, the Doctrine_Query object can handle conditions with any level of complexity. But since many developers think first in SQL before translating a condition into object-oriented logic, the `Doctrine_Query` object may be difficult to comprehend at first. The best way to understand it is to learn from examples and sample applications. The symfony project website, for instance, is full of `Doctrine_Query` building examples that will enlighten you in many ways.
359
368
360
369
Every `Doctrine_Query` instance has a `count()` method, which simply counts the number of records for the query and returns an integer. As there is no object to return, the hydrating process doesn't occur in this case, and the `count()` method is faster than `execute()`.
0 commit comments