Added setParamater calls to examples
Whitespace changes to break up the long SQL lines into a readable format
This commit is contained in:
parent
0411b1e75d
commit
fc6cec9074
@ -259,6 +259,7 @@ With WHERE Clause and Positional Parameter:
|
|||||||
|
|
||||||
<?php
|
<?php
|
||||||
$query = $em->createQuery('SELECT u FROM ForumUser u WHERE u.id = ?1');
|
$query = $em->createQuery('SELECT u FROM ForumUser u WHERE u.id = ?1');
|
||||||
|
$query->setParameter(1, 321);
|
||||||
$users = $query->getResult(); // array of ForumUser objects
|
$users = $query->getResult(); // array of ForumUser objects
|
||||||
|
|
||||||
With WHERE Clause and Named Parameter:
|
With WHERE Clause and Named Parameter:
|
||||||
@ -267,6 +268,7 @@ With WHERE Clause and Named Parameter:
|
|||||||
|
|
||||||
<?php
|
<?php
|
||||||
$query = $em->createQuery('SELECT u FROM ForumUser u WHERE u.username = :name');
|
$query = $em->createQuery('SELECT u FROM ForumUser u WHERE u.username = :name');
|
||||||
|
$query->setParameter(':name', 'Bob');
|
||||||
$users = $query->getResult(); // array of ForumUser objects
|
$users = $query->getResult(); // array of ForumUser objects
|
||||||
|
|
||||||
With Nested Conditions in WHERE Clause:
|
With Nested Conditions in WHERE Clause:
|
||||||
@ -275,6 +277,11 @@ With Nested Conditions in WHERE Clause:
|
|||||||
|
|
||||||
<?php
|
<?php
|
||||||
$query = $em->createQuery('SELECT u from ForumUser u WHERE (u.username = :name OR u.username = :name2) AND u.id = :id');
|
$query = $em->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
|
$users = $query->getResult(); // array of ForumUser objects
|
||||||
|
|
||||||
With COUNT DISTINCT:
|
With COUNT DISTINCT:
|
||||||
@ -324,6 +331,8 @@ BETWEEN in WHERE clause:
|
|||||||
|
|
||||||
<?php
|
<?php
|
||||||
$query = $em->createQuery('SELECT u.name FROM CmsUser u WHERE u.id BETWEEN ?1 AND ?2');
|
$query = $em->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();
|
$usernames = $query->getResult();
|
||||||
|
|
||||||
DQL Functions in WHERE clause:
|
DQL Functions in WHERE clause:
|
||||||
@ -354,9 +363,11 @@ CONCAT() DQL Function:
|
|||||||
|
|
||||||
<?php
|
<?php
|
||||||
$query = $em->createQuery("SELECT u.id FROM CmsUser u WHERE CONCAT(u.name, 's') = ?1");
|
$query = $em->createQuery("SELECT u.id FROM CmsUser u WHERE CONCAT(u.name, 's') = ?1");
|
||||||
|
$query->setParameter(1, 'Jess');
|
||||||
$ids = $query->getResult();
|
$ids = $query->getResult();
|
||||||
|
|
||||||
$query = $em->createQuery('SELECT CONCAT(u.id, u.name) FROM CmsUser u WHERE u.id = ?1');
|
$query = $em->createQuery('SELECT CONCAT(u.id, u.name) FROM CmsUser u WHERE u.id = ?1');
|
||||||
|
$query->setParameter(1, 321);
|
||||||
$idUsernames = $query->getResult();
|
$idUsernames = $query->getResult();
|
||||||
|
|
||||||
EXISTS in WHERE clause with correlated Subquery
|
EXISTS in WHERE clause with correlated Subquery
|
||||||
@ -705,7 +716,12 @@ entities looks like the following:
|
|||||||
|
|
||||||
.. code-block:: sql
|
.. 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
|
Now when persist a new ``Employee`` instance it will set the
|
||||||
discriminator value for us automatically:
|
discriminator value for us automatically:
|
||||||
@ -732,7 +748,9 @@ entities:
|
|||||||
|
|
||||||
.. code-block:: sql
|
.. 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
|
Class Table Inheritance
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
@ -769,8 +787,17 @@ you'll notice some differences:
|
|||||||
|
|
||||||
.. code-block:: sql
|
.. 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 Person (
|
||||||
CREATE TABLE Employee (id INT NOT NULL, department VARCHAR(50) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB;
|
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
|
ALTER TABLE Employee ADD FOREIGN KEY (id) REFERENCES Person(id) ON DELETE CASCADE
|
||||||
|
|
||||||
|
|
||||||
@ -784,7 +811,10 @@ automatically for you:
|
|||||||
|
|
||||||
.. code-block:: sql
|
.. 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
|
The Query class
|
||||||
@ -826,7 +856,7 @@ the Query class. Here they are:
|
|||||||
pure/mixed distinction does not apply.
|
pure/mixed distinction does not apply.
|
||||||
- ``Query#getArrayResult()``: Retrieves an array graph (a nested
|
- ``Query#getArrayResult()``: Retrieves an array graph (a nested
|
||||||
array) that is largely interchangeable with the object graph
|
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::
|
.. note::
|
||||||
|
|
||||||
@ -850,7 +880,7 @@ general-purpose method
|
|||||||
Using this method you can directly supply the hydration mode as the
|
Using this method you can directly supply the hydration mode as the
|
||||||
second parameter via one of the Query constants. In fact, the
|
second parameter via one of the Query constants. In fact, the
|
||||||
methods mentioned earlier are just convenient shortcuts for 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
|
internally invokes execute, passing in ``Query::HYDRATE_OBJECT`` as
|
||||||
the hydration mode.
|
the hydration mode.
|
||||||
|
|
||||||
@ -914,10 +944,10 @@ Here is how the result could look like:
|
|||||||
array
|
array
|
||||||
array
|
array
|
||||||
[0] => User (Object)
|
[0] => User (Object)
|
||||||
['nameUpper'] => "Roman"
|
['nameUpper'] => "ROMAN"
|
||||||
array
|
array
|
||||||
[0] => User (Object)
|
[0] => User (Object)
|
||||||
['nameUpper'] => "Jonathan"
|
['nameUpper'] => "JONATHAN"
|
||||||
...
|
...
|
||||||
|
|
||||||
And here is how you would access it in PHP code:
|
And here is how you would access it in PHP code:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user