Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 4da95a8

Browse files
jwagefabpot
authored andcommitted
Adding missing sql examples
1 parent 0dfc223 commit 4da95a8

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

gentle-introduction/en/08-Inside-the-Model-Layer-Doctrine.markdown

+10-1
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,17 @@ Listing 8-12 - Retrieving Records by Doctrine_Query with `createQuery()`--Empty
311311
$q = Doctrine_Core::getTable('Article')->createQuery();
312312
$articles = $q->execute();
313313

314+
// Will result in the following SQL query
315+
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+
314317
>**SIDEBAR**
315318
>Hydrating
316319
>
317320
>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.
318321
319322
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.
320323

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
322325

323326
[php]
324327
$q = Doctrine_Core::getTable('Comment')
@@ -327,6 +330,9 @@ Listing 8-13 - Retrieving Records by Criteria with `doSelect()`--Criteria with C
327330
->orderBy('c.created_at ASC');
328331
$comments = $q->execute();
329332

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+
330336
Table 8-1 compares the SQL syntax with the `Doctrine_Query` object syntax.
331337

332338
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
355361
->orderBy('c.created_at ASC');
356362
$comments = $q->execute();
357363

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+
358367
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.
359368

360369
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

Comments
 (0)