From fc6cec90745ffd1f6e7220ca6372f3c903c47c0e Mon Sep 17 00:00:00 2001 From: Ray Rehbein Date: Thu, 16 Dec 2010 06:15:14 +0800 Subject: [PATCH] Added setParamater calls to examples Whitespace changes to break up the long SQL lines into a readable format --- en/reference/dql-doctrine-query-language.rst | 48 ++++++++++++++++---- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/en/reference/dql-doctrine-query-language.rst b/en/reference/dql-doctrine-query-language.rst index 18c9a4568..d745d8a98 100644 --- a/en/reference/dql-doctrine-query-language.rst +++ b/en/reference/dql-doctrine-query-language.rst @@ -259,6 +259,7 @@ With WHERE Clause and Positional Parameter: createQuery('SELECT u FROM ForumUser u WHERE u.id = ?1'); + $query->setParameter(1, 321); $users = $query->getResult(); // array of ForumUser objects With WHERE Clause and Named Parameter: @@ -267,6 +268,7 @@ With WHERE Clause and Named Parameter: createQuery('SELECT u FROM ForumUser u WHERE u.username = :name'); + $query->setParameter(':name', 'Bob'); $users = $query->getResult(); // array of ForumUser objects With Nested Conditions in WHERE Clause: @@ -275,6 +277,11 @@ With Nested Conditions in WHERE Clause: createQuery('SELECT u from ForumUser u WHERE (u.username = :name OR u.username = :name2) AND u.id = :id'); + $query->setParameters(array( + ':name' => 'Bob', + ':name' => 'Alice', + ':id' => 321, + )); $users = $query->getResult(); // array of ForumUser objects With COUNT DISTINCT: @@ -324,6 +331,8 @@ BETWEEN in WHERE clause: createQuery('SELECT u.name FROM CmsUser u WHERE u.id BETWEEN ?1 AND ?2'); + $query->setParameter(1, 123); + $query->setParameter(2, 321); $usernames = $query->getResult(); DQL Functions in WHERE clause: @@ -354,9 +363,11 @@ CONCAT() DQL Function: createQuery("SELECT u.id FROM CmsUser u WHERE CONCAT(u.name, 's') = ?1"); + $query->setParameter(1, 'Jess'); $ids = $query->getResult(); $query = $em->createQuery('SELECT CONCAT(u.id, u.name) FROM CmsUser u WHERE u.id = ?1'); + $query->setParameter(1, 321); $idUsernames = $query->getResult(); EXISTS in WHERE clause with correlated Subquery @@ -705,7 +716,12 @@ entities looks like the following: .. code-block:: sql - CREATE TABLE Person (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR(50) NOT NULL, discr VARCHAR(255) NOT NULL, department VARCHAR(50) NOT NULL) + CREATE TABLE Person ( + id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + name VARCHAR(50) NOT NULL, + discr VARCHAR(255) NOT NULL, + department VARCHAR(50) NOT NULL + ) Now when persist a new ``Employee`` instance it will set the discriminator value for us automatically: @@ -732,7 +748,9 @@ entities: .. code-block:: sql - SELECT p0_.id AS id0, p0_.name AS name1, p0_.department AS department2, p0_.discr AS discr3 FROM Person p0_ WHERE (p0_.name = ?) AND p0_.discr IN ('employee') + SELECT p0_.id AS id0, p0_.name AS name1, p0_.department AS department2, + p0_.discr AS discr3 FROM Person p0_ + WHERE (p0_.name = ?) AND p0_.discr IN ('employee') Class Table Inheritance ~~~~~~~~~~~~~~~~~~~~~~~ @@ -769,8 +787,17 @@ you'll notice some differences: .. code-block:: sql - CREATE TABLE Person (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(50) NOT NULL, discr VARCHAR(255) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB; - CREATE TABLE Employee (id INT NOT NULL, department VARCHAR(50) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB; + CREATE TABLE Person ( + id INT AUTO_INCREMENT NOT NULL, + name VARCHAR(50) NOT NULL, + discr VARCHAR(255) NOT NULL, + PRIMARY KEY(id) + ) ENGINE = InnoDB; + CREATE TABLE Employee ( + id INT NOT NULL, + department VARCHAR(50) NOT NULL, + PRIMARY KEY(id) + ) ENGINE = InnoDB; ALTER TABLE Employee ADD FOREIGN KEY (id) REFERENCES Person(id) ON DELETE CASCADE @@ -784,7 +811,10 @@ automatically for you: .. code-block:: sql - SELECT p0_.id AS id0, p0_.name AS name1, e1_.department AS department2, p0_.discr AS discr3 FROM Employee e1_ INNER JOIN Person p0_ ON e1_.id = p0_.id WHERE p0_.name = ? + SELECT p0_.id AS id0, p0_.name AS name1, e1_.department AS department2, + p0_.discr AS discr3 + FROM Employee e1_ INNER JOIN Person p0_ ON e1_.id = p0_.id + WHERE p0_.name = ? The Query class @@ -826,7 +856,7 @@ the Query class. Here they are: pure/mixed distinction does not apply. - ``Query#getArrayResult()``: Retrieves an array graph (a nested array) that is largely interchangeable with the object graph - generated by ``Query#getResultList()`` for read-only purposes. + generated by ``Query#getResult()`` for read-only purposes. .. note:: @@ -850,7 +880,7 @@ general-purpose method Using this method you can directly supply the hydration mode as the second parameter via one of the Query constants. In fact, the methods mentioned earlier are just convenient shortcuts for the -execute method. For example, the method ``Query#getResultList()`` +execute method. For example, the method ``Query#getResult()`` internally invokes execute, passing in ``Query::HYDRATE_OBJECT`` as the hydration mode. @@ -914,10 +944,10 @@ Here is how the result could look like: array array [0] => User (Object) - ['nameUpper'] => "Roman" + ['nameUpper'] => "ROMAN" array [0] => User (Object) - ['nameUpper'] => "Jonathan" + ['nameUpper'] => "JONATHAN" ... And here is how you would access it in PHP code: