2010-11-02 00:03:50 +03:00
Doctrine Query Language
===========================
2010-06-03 22:13:39 +04:00
2010-12-03 22:13:10 +03:00
DQL stands for Doctrine Query Language and is an Object
2011-02-26 13:51:43 +03:00
Query Language derivate that is very similar to the Hibernate
2010-12-03 22:13:10 +03:00
Query Language (HQL) or the Java Persistence Query Language (JPQL).
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
In essence, DQL provides powerful querying capabilities over your
object model. Imagine all your objects lying around in some storage
(like an object database). When writing DQL queries, think about
querying that storage to pick a certain subset of your objects.
2010-06-03 22:13:39 +04:00
2010-12-03 22:13:10 +03:00
.. note ::
2010-06-03 22:13:39 +04:00
2010-12-03 22:13:10 +03:00
A common mistake for beginners is to mistake DQL for
2010-11-01 23:16:12 +03:00
being just some form of SQL and therefore trying to use table names
and column names or join arbitrary tables together in a query. You
need to think about DQL as a query language for your object model,
not for your relational schema.
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
DQL is case in-sensitive, except for namespace, class and field
names, which are case sensitive.
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
Types of DQL queries
--------------------
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
DQL as a query language has SELECT, UPDATE and DELETE constructs
that map to their corresponding SQL statement types. INSERT
statements are not allowed in DQL, because entities and their
relations have to be introduced into the persistence context
through `` EntityManager#persist() `` to ensure consistency of your
object model.
DQL SELECT statements are a very powerful way of retrieving parts
of your domain model that are not accessible via associations.
Additionally they allow to retrieve entities and their associations
2011-03-28 22:40:39 +04:00
in one single SQL select statement which can make a huge difference
2010-11-01 23:16:12 +03:00
in performance in contrast to using several queries.
DQL UPDATE and DELETE statements offer a way to execute bulk
changes on the entities of your domain model. This is often
necessary when you cannot load all the affected entities of a bulk
2010-06-03 22:13:39 +04:00
update into memory.
2010-11-01 23:16:12 +03:00
SELECT queries
--------------
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
DQL SELECT clause
~~~~~~~~~~~~~~~~~
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
The select clause of a DQL query specifies what appears in the
query result. The composition of all the expressions in the select
clause also influences the nature of the query result.
2010-06-03 22:13:39 +04:00
Here is an example that selects all users with an age > 20:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u FROM MyProject\Model\User u WHERE u.age > 20');
$users = $query->getResult();
Lets examine the query:
2010-11-01 23:16:12 +03:00
- `` u `` is a so called identification variable or alias that
refers to the `` MyProject\Model\User `` class. By placing this alias
in the SELECT clause we specify that we want all instances of the
2011-03-28 22:40:39 +04:00
User class that are matched by this query to appear in the query
2010-11-01 23:16:12 +03:00
result.
- The FROM keyword is always followed by a fully-qualified class
name which in turn is followed by an identification variable or
alias for that class name. This class designates a root of our
query from which we can navigate further via joins (explained
later) and path expressions.
- The expression `` u.age `` in the WHERE clause is a path
expression. Path expressions in DQL are easily identified by the
use of the '.' operator that is used for constructing paths. The
path expression `` u.age `` refers to the `` age `` field on the User
class.
The result of this query would be a list of User objects where all
users are older than 20.
The SELECT clause allows to specify both class identification
variables that signal the hydration of a complete entity class or
just fields of the entity using the syntax `` u.name `` . Combinations
of both are also allowed and it is possible to wrap both fields and
identification values into aggregation and DQL functions. Numerical
fields can be part of computations using mathematical operations.
See the sub-section on
`DQL Functions, Aggregates and Operations <#dqlfn> `_ on more
information.
Joins
~~~~~
A SELECT query can contain joins. There are 2 types of JOINs:
"Regular" Joins and "Fetch" Joins.
**Regular Joins** : Used to limit the results and/or compute
aggregate values.
**Fetch Joins** : In addition to the uses of regular joins: Used to
fetch related entities and include them in the hydrated result of a
query.
There is no special DQL keyword that distinguishes a regular join
from a fetch join. A join (be it an inner or outer join) becomes a
"fetch join" as soon as fields of the joined entity appear in the
SELECT part of the DQL query outside of an aggregate function.
Otherwise its a "regular join".
2010-06-03 22:13:39 +04:00
Example:
Regular join of the address:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery("SELECT u FROM User u JOIN u.address a WHERE a.city = 'Berlin'");
$users = $query->getResult();
Fetch join of the address:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery("SELECT u, a FROM User u JOIN u.address a WHERE a.city = 'Berlin'");
$users = $query->getResult();
2010-11-01 23:16:12 +03:00
When Doctrine hydrates a query with fetch-join it returns the class
in the FROM clause on the root level of the result array. In the
previous example an array of User instances is returned and the
address of each user is fetched and hydrated into the
`` User#address `` variable. If you access the address Doctrine does
not need to lazy load the association with another query.
2010-06-03 22:13:39 +04:00
2010-12-11 14:31:31 +03:00
.. note ::
2010-11-11 01:43:24 +03:00
2010-12-11 14:31:31 +03:00
Doctrine allows you to walk all the associations between
2010-11-01 23:16:12 +03:00
all the objects in your domain model. Objects that were not already
loaded from the database are replaced with lazy load proxy
instances. Non-loaded Collections are also replaced by lazy-load
instances that fetch all the contained objects upon first access.
However relying on the lazy-load mechanism leads to many small
queries executed against the database, which can significantly
affect the performance of your application. **Fetch Joins** are the
solution to hydrate most or all of the entities that you need in a
single SELECT query.
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
Named and Positional Parameters
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
DQL supports both named and positional parameters, however in
contrast to many SQL dialects positional parameters are specified
with numbers, for example "?1", "?2" and so on. Named parameters
are specified with ":name1", ":name2" and so on.
2010-06-03 22:13:39 +04:00
2010-12-21 00:12:24 +03:00
When referencing the parameters in `` Query#setParameter($param, $value) ``
2011-03-28 22:40:39 +04:00
both named and positional parameters are used **without** their prefixes.
2010-12-21 00:12:24 +03:00
2010-11-01 23:16:12 +03:00
DQL SELECT Examples
~~~~~~~~~~~~~~~~~~~
This section contains a large set of DQL queries and some
explanations of what is happening. The actual result also depends
on the hydration mode.
2010-06-03 22:13:39 +04:00
Hydrate all User entities:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u FROM MyProject\Model\User u');
$users = $query->getResult(); // array of User objects
Retrieve the IDs of all CmsUsers:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u.id FROM CmsUser u');
$ids = $query->getResult(); // array of CmsUser ids
Retrieve the IDs of all users that have written an article:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-07-24 01:39:36 +04:00
$query = $em->createQuery('SELECT DISTINCT u.id FROM CmsArticle a JOIN a.user u');
2010-06-03 22:13:39 +04:00
$ids = $query->getResult(); // array of CmsUser ids
2010-11-01 23:16:12 +03:00
Retrieve all articles and sort them by the name of the articles
users instance:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
<?php
2010-07-24 01:39:36 +04:00
$query = $em->createQuery('SELECT a FROM CmsArticle a JOIN a.user u ORDER BY u.name ASC');
2010-06-03 22:13:39 +04:00
$articles = $query->getResult(); // array of CmsArticle objects
Retrieve the Username and Name of a CmsUser:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u.username, u.name FROM CmsUser u');
2011-03-28 22:40:39 +04:00
$users = $query->getResults(); // array of CmsUser username and name values
2010-06-03 22:13:39 +04:00
echo $users[0]['username'];
Retrieve a ForumUser and his single associated entity:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u, a FROM ForumUser u JOIN u.avatar a');
$users = $query->getResult(); // array of ForumUser objects with the avatar association loaded
echo get_class($users[0]->getAvatar());
Retrieve a CmsUser and fetch join all the phonenumbers he has:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u, p FROM CmsUser u JOIN u.phonenumbers p');
$users = $query->getResult(); // array of CmsUser objects with the phonenumbers association loaded
$phonenumbers = $users[0]->getPhonenumbers();
Hydrate a result in Ascending:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u FROM ForumUser u ORDER BY u.id ASC');
$users = $query->getResult(); // array of ForumUser objects
Or in Descending Order:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u FROM ForumUser u ORDER BY u.id DESC');
$users = $query->getResult(); // array of ForumUser objects
Using Aggregate Functions:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT COUNT(u.id) FROM Entities\User u');
$count = $query->getSingleScalarResult();
2011-06-11 10:59:33 +04:00
$query = $em->createQuery('SELECT u, count(g.id) FROM Entities\User u JOIN u.groups g GROUP BY u.id');
2011-06-11 12:07:18 +04:00
$result = $query->getResult();
2011-06-11 10:59:33 +04:00
2010-06-03 22:13:39 +04:00
With WHERE Clause and Positional Parameter:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u FROM ForumUser u WHERE u.id = ?1');
2010-12-16 01:15:14 +03:00
$query->setParameter(1, 321);
2010-06-03 22:13:39 +04:00
$users = $query->getResult(); // array of ForumUser objects
With WHERE Clause and Named Parameter:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u FROM ForumUser u WHERE u.username = :name');
2011-03-28 22:40:39 +04:00
$query->setParameter('name', 'Bob');
2010-06-03 22:13:39 +04:00
$users = $query->getResult(); // array of ForumUser objects
With Nested Conditions in WHERE Clause:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u from ForumUser u WHERE (u.username = :name OR u.username = :name2) AND u.id = :id');
2010-12-16 01:15:14 +03:00
$query->setParameters(array(
2011-03-28 22:40:39 +04:00
'name' => 'Bob',
'name2' => 'Alice',
'id' => 321,
2010-12-16 01:15:14 +03:00
));
2010-06-03 22:13:39 +04:00
$users = $query->getResult(); // array of ForumUser objects
With COUNT DISTINCT:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-07-07 15:10:48 +04:00
$query = $em->createQuery('SELECT COUNT(DISTINCT u.name) FROM CmsUser');
2010-06-03 22:13:39 +04:00
$users = $query->getResult(); // array of ForumUser objects
With Arithmetic Expression in WHERE clause:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u FROM CmsUser u WHERE ((u.id + 5000) * u.id + 3) < 10000000');
$users = $query->getResult(); // array of ForumUser objects
2010-11-01 23:16:12 +03:00
Using a LEFT JOIN to hydrate all user-ids and optionally associated
article-ids:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u.id, a.id as article_id FROM CmsUser u LEFT JOIN u.articles a');
$results = $query->getResult(); // array of user ids and every article_id for each user
Restricting a JOIN clause by additional conditions:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery("SELECT u FROM CmsUser u LEFT JOIN u.articles a WITH a.topic LIKE '%foo%'");
$users = $query->getResult();
Using several Fetch JOINs:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u, a, p, c FROM CmsUser u JOIN u.articles a JOIN u.phonenumbers p JOIN a.comments c');
$users = $query->getResult();
BETWEEN in WHERE clause:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u.name FROM CmsUser u WHERE u.id BETWEEN ?1 AND ?2');
2010-12-16 01:15:14 +03:00
$query->setParameter(1, 123);
$query->setParameter(2, 321);
2010-06-03 22:13:39 +04:00
$usernames = $query->getResult();
DQL Functions in WHERE clause:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery("SELECT u.name FROM CmsUser u WHERE TRIM(u.name) = 'someone'");
$usernames = $query->getResult();
IN() Expression:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u.name FROM CmsUser u WHERE u.id IN(46)');
$usernames = $query->getResult();
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u FROM CmsUser u WHERE u.id IN (1, 2)');
$users = $query->getResult();
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u FROM CmsUser u WHERE u.id NOT IN (1)');
$users = $query->getResult();
CONCAT() DQL Function:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery("SELECT u.id FROM CmsUser u WHERE CONCAT(u.name, 's') = ?1");
2010-12-16 01:15:14 +03:00
$query->setParameter(1, 'Jess');
2010-06-03 22:13:39 +04:00
$ids = $query->getResult();
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT CONCAT(u.id, u.name) FROM CmsUser u WHERE u.id = ?1');
2010-12-16 01:15:14 +03:00
$query->setParameter(1, 321);
2010-06-03 22:13:39 +04:00
$idUsernames = $query->getResult();
EXISTS in WHERE clause with correlated Subquery
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-10-01 00:05:39 +04:00
$query = $em->createQuery('SELECT u.id FROM CmsUser u WHERE EXISTS (SELECT p.phonenumber FROM CmsPhonenumber p WHERE p.user = u.id)');
2010-06-03 22:13:39 +04:00
$ids = $query->getResult();
Get all users who are members of $group.
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-10-01 00:09:49 +04:00
$query = $em->createQuery('SELECT u.id FROM CmsUser u WHERE :groupId MEMBER OF u.groups');
2011-03-28 22:40:39 +04:00
$query->setParameter('groupId', $group);
2010-06-03 22:13:39 +04:00
$ids = $query->getResult();
Get all users that have more than 1 phonenumber
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u FROM CmsUser u WHERE SIZE(u.phonenumbers) > 1');
$users = $query->getResult();
Get all users that have no phonenumber
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u FROM CmsUser u WHERE u.phonenumbers IS EMPTY');
$users = $query->getResult();
2010-11-01 23:16:12 +03:00
Get all instances of a specific type, for use with inheritance
hierarchies:
2010-08-08 23:20:58 +04:00
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-08-08 23:20:58 +04:00
$query = $em->createQuery('SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF Doctrine\Tests\Models\Company\CompanyEmployee');
$query = $em->createQuery('SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF ?1');
2010-09-28 00:59:50 +04:00
$query = $em->createQuery('SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u NOT INSTANCE OF ?1');
2010-08-08 23:20:58 +04:00
2010-11-01 23:16:12 +03:00
Partial Object Syntax
^^^^^^^^^^^^^^^^^^^^^
By default when you run a DQL query in Doctrine and select only a
subset of the fields for a given entity, you do not receive objects
back. Instead, you receive only arrays as a flat rectangular result
set, similar to how you would if you were just using SQL directly
and joining some data.
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
If you want to select partial objects you can use the `` partial ``
DQL keyword:
2010-06-03 22:13:39 +04:00
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT partial u.{id, username} FROM CmsUser u');
$users = $query->getResult(); // array of partially loaded CmsUser objects
You use the partial syntax when joining as well:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT partial u.{id, username}, partial a.{id, name} FROM CmsUser u JOIN u.articles a');
$users = $query->getResult(); // array of partially loaded CmsUser objects
2010-11-01 23:16:12 +03:00
Using INDEX BY
~~~~~~~~~~~~~~
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
The INDEX BY construct is nothing that directly translates into SQL
but that affects object and array hydration. After each FROM and
JOIN clause you specify by which field this class should be indexed
in the result. By default a result is incremented by numerical keys
starting with 0. However with INDEX BY you can specify any other
column to be the key of your result, it really only makes sense
with primary or unique fields though:
2010-11-02 00:03:50 +03:00
.. code-block :: sql
2010-06-03 22:13:39 +04:00
SELECT u.id, u.status, upper(u.name) nameUpper FROM User u INDEX BY u.id
JOIN u.phonenumbers p INDEX BY p.phonenumber
2010-11-01 23:16:12 +03:00
Returns an array of the following kind, indexed by both user-id
then phonenumber-id:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
array
0 =>
array
1 =>
object(stdClass)[299]
public '__CLASS__' => string 'Doctrine\Tests\Models\CMS\CmsUser' (length=33)
public 'id' => int 1
..
'nameUpper' => string 'ROMANB' (length=6)
1 =>
array
2 =>
object(stdClass)[298]
public '__CLASS__' => string 'Doctrine\Tests\Models\CMS\CmsUser' (length=33)
public 'id' => int 2
...
'nameUpper' => string 'JWAGE' (length=5)
2010-11-01 23:16:12 +03:00
UPDATE queries
--------------
DQL not only allows to select your Entities using field names, you
can also execute bulk updates on a set of entities using an
DQL-UPDATE query. The Syntax of an UPDATE query works as expected,
as the following example shows:
2010-06-03 22:13:39 +04:00
2010-11-02 00:03:50 +03:00
.. code-block :: sql
2010-06-03 22:13:39 +04:00
UPDATE MyProject\Model\User u SET u.password = 'new' WHERE u.id IN (1, 2, 3)
2010-11-01 23:16:12 +03:00
References to related entities are only possible in the WHERE
clause and using sub-selects.
2010-06-03 22:13:39 +04:00
2010-11-02 00:03:50 +03:00
.. warning ::
2010-06-03 22:13:39 +04:00
2010-11-02 00:03:50 +03:00
DQL UPDATE statements are ported directly into a
Database UPDATE statement and therefore bypass any locking scheme, events
2010-11-01 23:16:12 +03:00
and do not increment the version column. Entities that are already
loaded into the persistence context will *NOT* be synced with the
updated database state. It is recommended to call
`` EntityManager#clear() `` and retrieve new instances of any
affected entity.
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
DELETE queries
--------------
DELETE queries can also be specified using DQL and their syntax is
as simple as the UPDATE syntax:
2010-11-02 00:03:50 +03:00
.. code-block :: sql
2010-06-03 22:13:39 +04:00
DELETE MyProject\Model\User u WHERE u.id = 4
The same restrictions apply for the reference of related entities.
2010-11-02 00:03:50 +03:00
.. warning ::
DQL DELETE statements are ported directly into a
Database DELETE statement and therefore bypass any events and checks for the
2010-11-01 23:16:12 +03:00
version column if they are not explicitly added to the WHERE clause
of the query. Additionally Deletes of specifies entities are *NOT*
cascaded to related entities even if specified in the metadata.
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
Functions, Operators, Aggregates
--------------------------------
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
DQL Functions
~~~~~~~~~~~~~
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
The following functions are supported in SELECT, WHERE and HAVING
clauses:
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
- ABS(arithmetic\_expression)
- CONCAT(str1, str2)
- CURRENT\_DATE() - Return the current date
- CURRENT\_TIME() - Returns the current time
- CURRENT\_TIMESTAMP() - Returns a timestamp of the current date
and time.
- LENGTH(str) - Returns the length of the given string
- LOCATE(needle, haystack [, offset]) - Locate the first
occurrence of the substring in the string.
- LOWER(str) - returns the string lowercased.
- MOD(a, b) - Return a MOD b.
- SIZE(collection) - Return the number of elements in the
specified collection
- SQRT(q) - Return the square-root of q.
- SUBSTRING(str, start [, length]) - Return substring of given
string.
- TRIM([LEADING \| TRAILING \| BOTH] ['trchar' FROM] str) - Trim
the string by the given trim char, defaults to whitespaces.
- UPPER(str) - Return the upper-case of the given string.
Arithmetic operators
~~~~~~~~~~~~~~~~~~~~
2010-06-03 22:13:39 +04:00
You can do math in DQL using numeric values, for example:
2010-12-03 22:13:10 +03:00
.. code-block :: sql
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
SELECT person.salary * 1.5 FROM CompanyPerson person WHERE person.salary < 100000
2010-11-01 23:16:12 +03:00
Aggregate Functions
~~~~~~~~~~~~~~~~~~~
The following aggregate functions are allowed in SELECT and GROUP
BY clauses: AVG, COUNT, MIN, MAX, SUM
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
Other Expressions
~~~~~~~~~~~~~~~~~
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
DQL offers a wide-range of additional expressions that are known
from SQL, here is a list of all the supported constructs:
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
- `` ALL/ANY/SOME `` - Used in a WHERE clause followed by a
sub-select this works like the equivalent constructs in SQL.
- `` BETWEEN a AND b `` and `` NOT BETWEEN a AND b `` can be used to
match ranges of arithmetic values.
- `` IN (x1, x2, ...) `` and `` NOT IN (x1, x2, ..) `` can be used to
match a set of given values.
- `` LIKE .. `` and `` NOT LIKE .. `` match parts of a string or text
using % as a wildcard.
- `` IS NULL `` and `` IS NOT NULL `` to check for null values
- `` EXISTS `` and `` NOT EXISTS `` in combination with a sub-select
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
Adding your own functions to the DQL language
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
By default DQL comes with functions that are part of a large basis
of underlying databases. However you will most likely choose a
database platform at the beginning of your project and most likely
never change it. For this cases you can easily extend the DQL
parser with own specialized platform functions.
2010-06-03 22:13:39 +04:00
You can register custom DQL functions in your ORM Configuration:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$config = new \Doctrine\ORM\Configuration();
$config->addCustomStringFunction($name, $class);
$config->addCustomNumericFunction($name, $class);
$config->addCustomDatetimeFunction($name, $class);
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
$em = EntityManager::create($dbParams, $config);
2010-11-01 23:16:12 +03:00
The functions have to return either a string, numeric or datetime
value depending on the registered function type. As an example we
will add a MySQL specific FLOOR() functionality. All the given
classes have to implement the base class :
2010-06-03 22:13:39 +04:00
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
<?php
namespace MyProject\Query\AST;
2010-06-03 22:13:39 +04:00
use \Doctrine\ORM\Query\AST\Functions\FunctionsNode;
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
class MysqlFloor extends FunctionNode
{
public $simpleArithmeticExpression;
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
{
return 'FLOOR(' . $sqlWalker->walkSimpleArithmeticExpression(
$this->simpleArithmeticExpression
) . ')';
}
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
public function parse(\Doctrine\ORM\Query\Parser $parser)
{
$lexer = $parser->getLexer();
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
$parser->match(Lexer::T_ABS);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
$this->simpleArithmeticExpression = $parser->SimpleArithmeticExpression();
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
}
We will register the function by calling and can then use it:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
\Doctrine\ORM\Query\Parser::registerNumericFunction('FLOOR', 'MyProject\Query\MysqlFloor');
$dql = "SELECT FLOOR(person.salary * 1.75) FROM CompanyPerson person";
2010-11-01 23:16:12 +03:00
Querying Inherited Classes
--------------------------
2010-06-17 20:03:01 +04:00
2010-11-01 23:16:12 +03:00
This section demonstrates how you can query inherited classes and
what type of results to expect.
2010-06-17 20:03:01 +04:00
2010-11-01 23:16:12 +03:00
Single Table
~~~~~~~~~~~~
2010-06-17 20:03:01 +04:00
2010-11-01 23:16:12 +03:00
`Single Table Inheritance <http://martinfowler.com/eaaCatalog/singleTableInheritance.html> `_
is an inheritance mapping strategy where all classes of a hierarchy
are mapped to a single database table. In order to distinguish
which row represents which type in the hierarchy a so-called
discriminator column is used.
2010-06-17 20:03:01 +04:00
2010-11-01 23:16:12 +03:00
First we need to setup an example set of entities to use. In this
scenario it is a generic Person and Employee example:
2010-06-17 20:03:01 +04:00
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-17 20:03:01 +04:00
2010-11-01 23:16:12 +03:00
<?php
namespace Entities;
2010-06-17 20:03:01 +04:00
/**
* @Entity
* @InheritanceType("SINGLE_TABLE")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
*/
class Person
{
/**
* @Id @Column(type="integer")
* @GeneratedValue
*/
protected $id;
2010-11-01 23:16:12 +03:00
2010-06-17 20:03:01 +04:00
/**
* @Column(type="string", length=50)
*/
protected $name;
2010-11-01 23:16:12 +03:00
2010-06-17 20:03:01 +04:00
// ...
}
2010-11-01 23:16:12 +03:00
2010-06-17 20:03:01 +04:00
/**
* @Entity
*/
class Employee extends Person
{
/**
* @Column(type="string", length=50)
*/
private $department;
2010-11-01 23:16:12 +03:00
2010-06-17 20:03:01 +04:00
// ...
}
2010-11-01 23:16:12 +03:00
First notice that the generated SQL to create the tables for these
entities looks like the following:
2010-11-02 00:03:50 +03:00
.. code-block :: sql
2010-06-17 20:03:01 +04:00
2010-12-16 01:15:14 +03:00
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
)
2010-06-17 20:03:01 +04:00
2010-11-01 23:16:12 +03:00
Now when persist a new `` Employee `` instance it will set the
discriminator value for us automatically:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-17 20:03:01 +04:00
2010-11-01 23:16:12 +03:00
<?php
2010-06-17 20:03:01 +04:00
$employee = new \Entities\Employee();
$employee->setName('test');
$employee->setDepartment('testing');
$em->persist($employee);
$em->flush();
2010-11-01 23:16:12 +03:00
Now lets run a simple query to retrieve the `` Employee `` we just
created:
2010-11-02 00:03:50 +03:00
.. code-block :: sql
2010-06-17 20:03:01 +04:00
2010-06-17 21:32:11 +04:00
SELECT e FROM Entities\Employee e WHERE e.name = 'test'
2010-06-17 20:03:01 +04:00
2010-11-01 23:16:12 +03:00
If we check the generated SQL you will notice it has some special
conditions added to ensure that we will only get back `` Employee ``
entities:
2010-11-02 00:03:50 +03:00
.. code-block :: sql
2010-06-17 20:03:01 +04:00
2010-12-16 01:15:14 +03:00
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')
2010-06-17 20:03:01 +04:00
2010-11-01 23:16:12 +03:00
Class Table Inheritance
~~~~~~~~~~~~~~~~~~~~~~~
`Class Table Inheritance <http://martinfowler.com/eaaCatalog/classTableInheritance.html> `_
is an inheritance mapping strategy where each class in a hierarchy
is mapped to several tables: its own table and the tables of all
parent classes. The table of a child class is linked to the table
of a parent class through a foreign key constraint. Doctrine 2
implements this strategy through the use of a discriminator column
in the topmost table of the hierarchy because this is the easiest
way to achieve polymorphic queries with Class Table Inheritance.
2010-06-17 20:03:01 +04:00
2010-11-01 23:16:12 +03:00
The example for class table inheritance is the same as single
table, you just need to change the inheritance type from
`` SINGLE_TABLE `` to `` JOINED `` :
2010-06-17 20:03:01 +04:00
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-17 20:03:01 +04:00
2010-11-01 23:16:12 +03:00
<?php
2010-06-17 20:03:01 +04:00
/**
* @Entity
* @InheritanceType("JOINED")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
*/
class Person
{
// ...
}
2010-11-01 23:16:12 +03:00
Now take a look at the SQL which is generated to create the table,
you'll notice some differences:
2010-11-02 00:03:50 +03:00
.. code-block :: sql
2010-06-17 20:03:01 +04:00
2010-12-16 01:15:14 +03:00
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;
2010-06-17 20:03:01 +04:00
ALTER TABLE Employee ADD FOREIGN KEY (id) REFERENCES Person(id) ON DELETE CASCADE
2010-11-01 23:16:12 +03:00
- The data is split between two tables
- A foreign key exists between the two tables
Now if were to insert the same `` Employee `` as we did in the
`` SINGLE_TABLE `` example and run the same example query it will
generate different SQL joining the `` Person `` information
2010-06-17 20:03:01 +04:00
automatically for you:
2010-11-02 00:03:50 +03:00
.. code-block :: sql
2010-11-01 23:16:12 +03:00
2010-12-16 01:15:14 +03:00
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 = ?
2010-06-17 20:03:01 +04:00
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
The Query class
---------------
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
An instance of the `` Doctrine\ORM\Query `` class represents a DQL
query. You create a Query instance be calling
`` EntityManager#createQuery($dql) `` , passing the DQL query string.
Alternatively you can create an empty `` Query `` instance and invoke
`` Query#setDql($dql) `` afterwards. Here are some examples:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
<?php
// $em instanceof EntityManager
2010-06-03 22:13:39 +04:00
// example1: passing a DQL string
$q = $em->createQuery('select u from MyProject\Model\User u');
2010-11-01 23:16:12 +03:00
2010-10-06 01:43:56 +04:00
// example2: using setDql
2010-06-03 22:13:39 +04:00
$q = $em->createQuery();
$q->setDql('select u from MyProject\Model\User u');
2010-11-01 23:16:12 +03:00
Query Result Formats
~~~~~~~~~~~~~~~~~~~~
The format in which the result of a DQL SELECT query is returned
can be influenced by a so-called `` hydration mode `` . A hydration
mode specifies a particular way in which an SQL result set is
transformed. Each hydration mode has its own dedicated method on
the Query class. Here they are:
- `` Query#getResult() `` : Retrieves a collection of objects. The
result is either a plain collection of objects (pure) or an array
where the objects are nested in the result rows (mixed).
- `` Query#getSingleResult() `` : Retrieves a single object. If the
result contains more than one object, an exception is thrown. The
pure/mixed distinction does not apply.
- `` Query#getArrayResult() `` : Retrieves an array graph (a nested
array) that is largely interchangeable with the object graph
2010-12-16 01:15:14 +03:00
generated by `` Query#getResult() `` for read-only purposes.
2010-11-01 23:16:12 +03:00
2010-12-03 22:13:10 +03:00
.. note ::
An array graph can differ from the corresponding object
graph in certain scenarios due to the difference of the identity
semantics between arrays and objects.
2010-11-01 23:16:12 +03:00
- `` Query#getScalarResult() `` : Retrieves a flat/rectangular result
set of scalar values that can contain duplicate data. The
pure/mixed distinction does not apply.
- `` Query#getSingleScalarResult() `` : Retrieves a single scalar
value from the result returned by the dbms. If the result contains
more than a single scalar value, an exception is thrown. The
pure/mixed distinction does not apply.
Instead of using these methods, you can alternatively use the
general-purpose method
`` Query#execute(array $params = array(), $hydrationMode = Query::HYDRATE_OBJECT) `` .
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
2010-12-16 01:15:14 +03:00
execute method. For example, the method `` Query#getResult() ``
2010-11-01 23:16:12 +03:00
internally invokes execute, passing in `` Query::HYDRATE_OBJECT `` as
the hydration mode.
The use of the methods mentioned earlier is generally preferred as
it leads to more concise code.
Pure and Mixed Results
~~~~~~~~~~~~~~~~~~~~~~
The nature of a result returned by a DQL SELECT query retrieved
through `` Query#getResult() `` or `` Query#getArrayResult() `` can be
of 2 forms: **pure** and **mixed** . In the previous simple
examples, you already saw a "pure" query result, with only objects.
By default, the result type is **pure** but
**as soon as scalar values, such as aggregate values or other scalar values that do not belong to an entity, appear in the SELECT part of the DQL query, the result becomes mixed** .
A mixed result has a different structure than a pure result in
order to accommodate for the scalar values.
2010-06-03 22:13:39 +04:00
A pure result usually looks like this:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
2011-04-03 22:00:43 +04:00
$dql = "SELECT u FROM User u";
2010-06-03 22:13:39 +04:00
array
[0] => Object
[1] => Object
[2] => Object
...
2010-11-01 23:16:12 +03:00
A mixed result on the other hand has the following general
structure:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
2011-06-11 10:59:33 +04:00
$dql = "SELECT u, 'some scalar string', count(u.groups) AS num FROM User u JOIN u.groups g GROUP BY u.id";
2011-04-03 22:00:43 +04:00
2010-06-03 22:13:39 +04:00
array
2011-03-28 22:40:39 +04:00
[0]
2010-06-03 22:13:39 +04:00
[0] => Object
[1] => "some scalar string"
2011-04-03 22:00:43 +04:00
['num'] => 42
2010-06-03 22:13:39 +04:00
// ... more scalar values, either indexed numerically or with a name
2011-03-28 22:40:39 +04:00
[1]
2010-06-03 22:13:39 +04:00
[0] => Object
[1] => "some scalar string"
2011-04-03 22:00:43 +04:00
['num'] => 42
2010-06-03 22:13:39 +04:00
// ... more scalar values, either indexed numerically or with a name
2010-11-01 23:16:12 +03:00
To better understand mixed results, consider the following DQL
query:
2010-11-02 00:03:50 +03:00
.. code-block :: sql
2010-06-03 22:13:39 +04:00
SELECT u, UPPER(u.name) nameUpper FROM MyProject\Model\User u
2010-11-01 23:16:12 +03:00
This query makes use of the `` UPPER `` DQL function that returns a
scalar value and because there is now a scalar value in the SELECT
clause, we get a mixed result.
2010-06-03 22:13:39 +04:00
2011-04-03 22:00:43 +04:00
Conventions for mixed results are as follows:
2011-04-03 22:13:34 +04:00
2011-04-03 22:00:43 +04:00
- The object fetched in the FROM clause is always positioned with the key '0'.
- Every scalar without a name is numbered in the order given in the query, starting with 1.
- Every aliased scalar is given with its alias-name as the key. The case of the name is kept.
- If several objects are fetched from the FROM clause they alternate every row.
2011-04-03 22:13:34 +04:00
2010-06-03 22:13:39 +04:00
Here is how the result could look like:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
array
array
[0] => User (Object)
2010-12-16 01:15:14 +03:00
['nameUpper'] => "ROMAN"
2010-06-03 22:13:39 +04:00
array
[0] => User (Object)
2010-12-16 01:15:14 +03:00
['nameUpper'] => "JONATHAN"
2010-06-03 22:13:39 +04:00
...
And here is how you would access it in PHP code:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
foreach ($results as $row) {
echo "Name: " . $row[0]->getName();
echo "Name UPPER: " . $row['nameUpper'];
}
2011-04-03 22:00:43 +04:00
Fetching Multiple FROM Entities
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If you fetch multiple entities that are listed in the FROM clause then the hydration
will return the rows iterating the different top-level entities.
.. code-block :: php
$dql = "SELECT u, g FROM User u, Group g";
array
[0] => Object (User)
[1] => Object (Group)
[2] => Object (User)
[3] => Object (Group)
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
Hydration Modes
~~~~~~~~~~~~~~~
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
Each of the Hydration Modes makes assumptions about how the result
is returned to user land. You should know about all the details to
make best use of the different result formats:
2010-06-03 22:13:39 +04:00
The constants for the different hydration modes are:
2010-11-01 23:16:12 +03:00
- Query::HYDRATE\_OBJECT
- Query::HYDRATE\_ARRAY
- Query::HYDRATE\_SCALAR
- Query::HYDRATE\_SINGLE\_SCALAR
Object Hydration
^^^^^^^^^^^^^^^^
2010-06-03 22:13:39 +04:00
Object hydration hydrates the result set into the object graph:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u FROM CmsUser u');
$users = $query->getResult(Query::HYDRATE_OBJECT);
2010-11-01 23:16:12 +03:00
Array Hydration
^^^^^^^^^^^^^^^
You can run the same query with array hydration and the result set
is hydrated into an array that represents the object graph:
2010-06-03 22:13:39 +04:00
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u FROM CmsUser u');
$users = $query->getResult(Query::HYDRATE_ARRAY);
2010-11-01 23:16:12 +03:00
You can use the `` getArrayResult() `` shortcut as well:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$users = $query->getArrayResult();
2010-11-01 23:16:12 +03:00
Scalar Hydration
^^^^^^^^^^^^^^^^
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
If you want to return a flat rectangular result set instead of an
object graph you can use scalar hydration:
2010-06-03 22:13:39 +04:00
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u FROM CmsUser u');
$users = $query->getResult(Query::HYDRATE_SCALAR);
echo $users[0]['u_id'];
2010-11-01 23:16:12 +03:00
The following assumptions are made about selected fields using
Scalar Hydration:
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
1. Fields from classes are prefixed by the DQL alias in the result.
A query of the kind 'SELECT u.name ..' returns a key 'u\_name' in
the result rows.
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
Single Scalar Hydration
^^^^^^^^^^^^^^^^^^^^^^^
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
If you a query which returns just a single scalar value you can use
single scalar hydration:
2010-06-03 22:13:39 +04:00
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT COUNT(a.id) FROM CmsUser u LEFT JOIN u.articles a WHERE u.username = ?1 GROUP BY u.id');
$query->setParameter(1, 'jwage');
2010-06-08 03:54:22 +04:00
$numArticles = $query->getResult(Query::HYDRATE_SINGLE_SCALAR);
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
You can use the `` getSingleScalarResult() `` shortcut as well:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$numArticles = $query->getSingleScalarResult();
2010-11-01 23:16:12 +03:00
Custom Hydration Modes
^^^^^^^^^^^^^^^^^^^^^^
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
You can easily add your own custom hydration modes by first
creating a class which extends `` AbstractHydrator `` :
2010-06-03 22:13:39 +04:00
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
<?php
namespace MyProject\Hydrators;
2010-06-03 22:13:39 +04:00
use Doctrine\ORM\Internal\Hydration\AbstractHydrator;
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
class CustomHydrator extends AbstractHydrator
{
protected function _hydrateAll()
{
return $this->_stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
Next you just need to add the class to the ORM configuration:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$em->getConfiguration()->addCustomHydrationMode('CustomHydrator', 'MyProject\Hydrators\CustomHydrator');
Now the hydrator is ready to be used in your queries:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u FROM CmsUser u');
$results = $query->getResult('CustomHydrator');
2010-11-01 23:16:12 +03:00
Iterating Large Result Sets
~~~~~~~~~~~~~~~~~~~~~~~~~~~
There are situations when a query you want to execute returns a
very large result-set that needs to be processed. All the
previously described hydration modes completely load a result-set
into memory which might not be feasible with large result sets. See
the `Batch Processing <batch-processing> `_ section on details how
to iterate large result sets.
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
Functions
~~~~~~~~~
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
The following methods exist on the `` AbstractQuery `` which both
`` Query `` and `` NativeQuery `` extend from.
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
Parameters
^^^^^^^^^^
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
Prepared Statements that use numerical or named wildcards require
additional parameters to be executable against the database. To
pass parameters to the query the following methods can be used:
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
- `` AbstractQuery::setParameter($param, $value) `` - Set the
numerical or named wildcard to the given value.
- `` AbstractQuery::setParameters(array $params) `` - Set an array
of parameter key-value pairs.
- `` AbstractQuery::getParameter($param) ``
- `` AbstractQuery::getParameters() ``
2010-06-03 22:13:39 +04:00
2010-12-21 00:19:02 +03:00
Both named and positional parameters are passed to these methods without their ? or : prefix.
2010-11-01 23:16:12 +03:00
Cache related API
^^^^^^^^^^^^^^^^^
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
You can cache query results based either on all variables that
define the result (SQL, Hydration Mode, Parameters and Hints) or on
user-defined cache keys. However by default query results are not
cached at all. You have to enable the result cache on a per query
basis. The following example shows a complete workflow using the
Result Cache API:
2010-06-03 22:13:39 +04:00
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2010-06-03 22:13:39 +04:00
$query = $em->createQuery('SELECT u FROM MyProject\Model\User u WHERE u.id = ?1');
$query->setParameter(1, 12);
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
$query->setResultCacheDriver(new ApcCache());
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
$query->useResultCache(true)
->setResultCacheLifeTime($seconds = 3600);
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
$result = $query->getResult(); // cache miss
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
$query->expireResultCache(true);
$result = $query->getResult(); // forced expire, cache miss
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
$query->setResultCacheId('my_query_result');
$result = $query->getResult(); // saved in given result cache id.
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
// or call useResultCache() with all parameters:
$query->useResultCache(true, $seconds = 3600, 'my_query_result');
$result = $query->getResult(); // cache hit!
2010-11-01 23:16:12 +03:00
**TIP!** You can set the Result Cache Driver globally on the
`` Doctrine\ORM\Configuration `` instance so that it is passed to
every `` Query `` and `` NativeQuery `` instance.
Query Hints
^^^^^^^^^^^
You can pass hints to the query parser and hydrators by using the
`` AbstractQuery::setHint($name, $value) `` method. Currently there
exist mostly internal query hints that are not be consumed in
userland. However the following few hints are to be used in
userland:
- Query::HINT\_FORCE\_PARTIAL\_LOAD - Allows to hydrate objects
although not all their columns are fetched. This query hint can be
used to handle memory consumption problems with large result-sets
that contain char or binary data. Doctrine has no way of implicitly
reloading this data. Partially loaded objects have to be passed to
`` EntityManager::refresh() `` if they are to be reloaded fully from
the database.
- Query::HINT\_REFRESH - This query is used internally by
`` EntityManager::refresh() `` and can be used in userland as well.
If you specify this hint and a query returns the data for an entity
that is already managed by the UnitOfWork, the fields of the
existing entity will be refreshed. In normal operation a result-set
that loads data of an already existing entity is discarded in favor
of the already existing entity.
- Query::HINT\_CUSTOM\_TREE\_WALKERS - An array of additional
`` Doctrine\ORM\Query\TreeWalker `` instances that are attached to
the DQL query parsing process.
Query Cache (DQL Query Only)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Parsing a DQL query and converting it into an SQL query against the
underlying database platform obviously has some overhead in
contrast to directly executing Native SQL queries. That is why
there is a dedicated Query Cache for caching the DQL parser
results. In combination with the use of wildcards you can reduce
the number of parsed queries in production to zero.
The Query Cache Driver is passed from the
`` Doctrine\ORM\Configuration `` instance to each
`` Doctrine\ORM\Query `` instance by default and is also enabled by
default. This also means you don't regularly need to fiddle with
the parameters of the Query Cache, however if you do there are
several methods to interact with it:
- `` Query::setQueryCacheDriver($driver) `` - Allows to set a Cache
instance
- `` Query::setQueryCacheLifeTime($seconds = 3600) `` - Set lifetime
of the query caching.
- `` Query::expireQueryCache($bool) `` - Enforce the expiring of the
query cache if set to true.
- `` Query::getExpireQueryCache() ``
- `` Query::getQueryCacheDriver() ``
- `` Query::getQueryCacheLifeTime() ``
First and Max Result Items (DQL Query Only)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You can limit the number of results returned from a DQL query as
well as specify the starting offset, Doctrine then uses a strategy
of manipulating the select query to return only the requested
number of results:
- `` Query::setMaxResults($maxResults) ``
- `` Query::setFirstResult($offset) ``
2010-11-02 00:03:50 +03:00
.. note ::
If your query contains a fetch-joined collection
2010-11-01 23:16:12 +03:00
specifying the result limit methods are not working as you would
expect. Set Max Results restricts the number of database result
rows, however in the case of fetch-joined collections one root
entity might appear in many rows, effectively hydrating less than
the specified number of results.
EBNF
----
The following context-free grammar, written in an EBNF variant,
describes the Doctrine Query Language. You can consult this grammar
whenever you are unsure about what is possible with DQL or what the
correct syntax for a particular query should be.
Document syntax:
~~~~~~~~~~~~~~~~
- non-terminals begin with an upper case character
- terminals begin with a lower case character
- parentheses (...) are used for grouping
- square brackets [...] are used for defining an optional part,
e.g. zero or one time
- curly brackets {...} are used for repetition, e.g. zero or more
times
- double quotation marks "..." define a terminal string a vertical
bar \| represents an alternative
Terminals
~~~~~~~~~
- identifier (name, email, ...)
- string ('foo', 'bar''s house', '%ninja%', ...)
- char ('/', '\\', ' ', ...)
- integer (-1, 0, 1, 34, ...)
- float (-0.23, 0.007, 1.245342E+8, ...)
- boolean (false, true)
Query Language
~~~~~~~~~~~~~~
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
QueryLanguage ::= SelectStatement | UpdateStatement | DeleteStatement
2010-11-01 23:16:12 +03:00
Statements
~~~~~~~~~~
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
SelectStatement ::= SelectClause FromClause [WhereClause] [GroupByClause] [HavingClause] [OrderByClause]
UpdateStatement ::= UpdateClause [WhereClause]
DeleteStatement ::= DeleteClause [WhereClause]
2010-11-01 23:16:12 +03:00
Identifiers
~~~~~~~~~~~
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
/* Alias Identification usage (the "u" of "u.name") * /
IdentificationVariable ::= identifier
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
/* Alias Identification declaration (the "u" of "FROM User u") * /
AliasIdentificationVariable :: = identifier
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
/* identifier that must be a class name (the "User" of "FROM User u") * /
AbstractSchemaName ::= identifier
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
/* identifier that must be a field (the "name" of "u.name") * /
2010-10-06 01:43:56 +04:00
/* This is responsible to know if the field exists in Object, no matter if it's a relation or a simple field * /
2010-06-03 22:13:39 +04:00
FieldIdentificationVariable ::= identifier
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
/* identifier that must be a collection-valued association field (to-many) (the "Phonenumbers" of "u.Phonenumbers") * /
CollectionValuedAssociationField ::= FieldIdentificationVariable
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
/* identifier that must be a single-valued association field (to-one) (the "Group" of "u.Group") * /
SingleValuedAssociationField ::= FieldIdentificationVariable
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
/* identifier that must be an embedded class state field (for the future) * /
EmbeddedClassStateField ::= FieldIdentificationVariable
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
/* identifier that must be a simple state field (name, email, ...) (the "name" of "u.name") * /
/* The difference between this and FieldIdentificationVariable is only semantical, because it points to a single field (not mapping to a relation) * /
SimpleStateField ::= FieldIdentificationVariable
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
/* Alias ResultVariable declaration (the "total" of "COUNT(* ) AS total") */
AliasResultVariable = identifier
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
/* ResultVariable identifier usage of mapped field aliases (the "total" of "COUNT(* ) AS total") */
ResultVariable = identifier
2010-11-01 23:16:12 +03:00
Path Expressions
~~~~~~~~~~~~~~~~
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
/* "u.Group" or "u.Phonenumbers" declarations * /
JoinAssociationPathExpression ::= IdentificationVariable "." (CollectionValuedAssociationField | SingleValuedAssociationField)
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
/* "u.Group" or "u.Phonenumbers" usages * /
AssociationPathExpression ::= CollectionValuedPathExpression | SingleValuedAssociationPathExpression
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
/* "u.name" or "u.Group" * /
SingleValuedPathExpression ::= StateFieldPathExpression | SingleValuedAssociationPathExpression
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
/* "u.name" or "u.Group.name" * /
StateFieldPathExpression ::= IdentificationVariable "." StateField | SingleValuedAssociationPathExpression "." StateField
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
/* "u.Group" * /
2010-11-10 00:19:07 +03:00
SingleValuedAssociationPathExpression ::= IdentificationVariable "." SingleValuedAssociationField
2010-06-03 22:13:39 +04:00
/* "u.Group.Permissions" * /
CollectionValuedPathExpression ::= IdentificationVariable "." {SingleValuedAssociationField "."}* CollectionValuedAssociationField
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
/* "name" * /
StateField ::= {EmbeddedClassStateField "."}* SimpleStateField
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
/* "u.name" or "u.address.zip" (address = EmbeddedClassStateField) * /
SimpleStateFieldPathExpression ::= IdentificationVariable "." StateField
2010-11-01 23:16:12 +03:00
Clauses
~~~~~~~
2010-06-03 22:13:39 +04:00
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
SelectClause ::= "SELECT" ["DISTINCT"] SelectExpression {"," SelectExpression}*
SimpleSelectClause ::= "SELECT" ["DISTINCT"] SimpleSelectExpression
UpdateClause ::= "UPDATE" AbstractSchemaName ["AS"] AliasIdentificationVariable "SET" UpdateItem {"," UpdateItem}*
DeleteClause ::= "DELETE" ["FROM"] AbstractSchemaName ["AS"] AliasIdentificationVariable
FromClause ::= "FROM" IdentificationVariableDeclaration {"," IdentificationVariableDeclaration}*
SubselectFromClause ::= "FROM" SubselectIdentificationVariableDeclaration {"," SubselectIdentificationVariableDeclaration}*
WhereClause ::= "WHERE" ConditionalExpression
HavingClause ::= "HAVING" ConditionalExpression
GroupByClause ::= "GROUP" "BY" GroupByItem {"," GroupByItem}*
OrderByClause ::= "ORDER" "BY" OrderByItem {"," OrderByItem}*
Subselect ::= SimpleSelectClause SubselectFromClause [WhereClause] [GroupByClause] [HavingClause] [OrderByClause]
2010-11-01 23:16:12 +03:00
Items
~~~~~
2010-06-03 22:13:39 +04:00
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
UpdateItem ::= IdentificationVariable "." (StateField | SingleValuedAssociationField) "=" NewValue
OrderByItem ::= (ResultVariable | StateFieldPathExpression) ["ASC" | "DESC"]
GroupByItem ::= IdentificationVariable | SingleValuedPathExpression
NewValue ::= ScalarExpression | SimpleEntityExpression | "NULL"
2010-11-01 23:16:12 +03:00
From, Join and Index by
~~~~~~~~~~~~~~~~~~~~~~~
2010-06-03 22:13:39 +04:00
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
IdentificationVariableDeclaration ::= RangeVariableDeclaration [IndexBy] {JoinVariableDeclaration}*
SubselectIdentificationVariableDeclaration ::= IdentificationVariableDeclaration | (AssociationPathExpression ["AS"] AliasIdentificationVariable)
JoinVariableDeclaration ::= Join [IndexBy]
RangeVariableDeclaration ::= AbstractSchemaName ["AS"] AliasIdentificationVariable
Join ::= ["LEFT" ["OUTER"] | "INNER"] "JOIN" JoinAssociationPathExpression
["AS"] AliasIdentificationVariable ["WITH" ConditionalExpression]
IndexBy ::= "INDEX" "BY" SimpleStateFieldPathExpression
2010-11-01 23:16:12 +03:00
Select Expressions
~~~~~~~~~~~~~~~~~~
2010-06-03 22:13:39 +04:00
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
SelectExpression ::= IdentificationVariable | PartialObjectExpression | (AggregateExpression | "(" Subselect ")" | FunctionDeclaration | ScalarExpression) [["AS"] AliasResultVariable]
SimpleSelectExpression ::= ScalarExpression | IdentificationVariable |
(AggregateExpression [["AS"] AliasResultVariable])
PartialObjectExpression ::= "PARTIAL" IdentificationVariable "." PartialFieldSet
PartialFieldSet ::= "{" SimpleStateField {"," SimpleStateField}* "}"
2010-11-01 23:16:12 +03:00
Conditional Expressions
~~~~~~~~~~~~~~~~~~~~~~~
2010-06-03 22:13:39 +04:00
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
ConditionalExpression ::= ConditionalTerm {"OR" ConditionalTerm}*
ConditionalTerm ::= ConditionalFactor {"AND" ConditionalFactor}*
ConditionalFactor ::= ["NOT"] ConditionalPrimary
ConditionalPrimary ::= SimpleConditionalExpression | "(" ConditionalExpression ")"
SimpleConditionalExpression ::= ComparisonExpression | BetweenExpression | LikeExpression |
InExpression | NullComparisonExpression | ExistsExpression |
EmptyCollectionComparisonExpression | CollectionMemberExpression
2010-11-01 23:16:12 +03:00
Collection Expressions
~~~~~~~~~~~~~~~~~~~~~~
2010-06-03 22:13:39 +04:00
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
EmptyCollectionComparisonExpression ::= CollectionValuedPathExpression "IS" ["NOT"] "EMPTY"
CollectionMemberExpression ::= EntityExpression ["NOT"] "MEMBER" ["OF"] CollectionValuedPathExpression
2010-11-01 23:16:12 +03:00
Literal Values
~~~~~~~~~~~~~~
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
Literal ::= string | char | integer | float | boolean
InParameter ::= Literal | InputParameter
2010-11-01 23:16:12 +03:00
Input Parameter
~~~~~~~~~~~~~~~
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
InputParameter ::= PositionalParameter | NamedParameter
PositionalParameter ::= "?" integer
NamedParameter ::= ":" string
2010-11-01 23:16:12 +03:00
Arithmetic Expressions
~~~~~~~~~~~~~~~~~~~~~~
2010-06-03 22:13:39 +04:00
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
ArithmeticExpression ::= SimpleArithmeticExpression | "(" Subselect ")"
SimpleArithmeticExpression ::= ArithmeticTerm {("+" | "-") ArithmeticTerm}*
ArithmeticTerm ::= ArithmeticFactor {("*" | "/") ArithmeticFactor}*
ArithmeticFactor ::= [("+" | "-")] ArithmeticPrimary
ArithmeticPrimary ::= SingleValuedPathExpression | Literal | "(" SimpleArithmeticExpression ")"
| FunctionsReturningNumerics | AggregateExpression | FunctionsReturningStrings
| FunctionsReturningDatetime | IdentificationVariable | InputParameter
2010-11-01 23:16:12 +03:00
Scalar and Type Expressions
~~~~~~~~~~~~~~~~~~~~~~~~~~~
2010-06-03 22:13:39 +04:00
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
ScalarExpression ::= SimpleArithmeticExpression | StringPrimary | DateTimePrimary | StateFieldPathExpression
2011-03-20 14:05:28 +03:00
BooleanPrimary | EntityTypeExpression
2010-06-03 22:13:39 +04:00
StringExpression ::= StringPrimary | "(" Subselect ")"
StringPrimary ::= StateFieldPathExpression | string | InputParameter | FunctionsReturningStrings | AggregateExpression
BooleanExpression ::= BooleanPrimary | "(" Subselect ")"
BooleanPrimary ::= StateFieldPathExpression | boolean | InputParameter
EntityExpression ::= SingleValuedAssociationPathExpression | SimpleEntityExpression
SimpleEntityExpression ::= IdentificationVariable | InputParameter
DatetimeExpression ::= DatetimePrimary | "(" Subselect ")"
DatetimePrimary ::= StateFieldPathExpression | InputParameter | FunctionsReturningDatetime | AggregateExpression
2011-03-20 14:05:28 +03:00
.. note ::
Parts of CASE expressions are not yet implemented.
2010-11-01 23:16:12 +03:00
Aggregate Expressions
~~~~~~~~~~~~~~~~~~~~~
2010-06-03 22:13:39 +04:00
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
AggregateExpression ::= ("AVG" | "MAX" | "MIN" | "SUM") "(" ["DISTINCT"] StateFieldPathExpression ")" |
"COUNT" "(" ["DISTINCT"] (IdentificationVariable | SingleValuedPathExpression) ")"
2010-11-01 23:16:12 +03:00
Other Expressions
~~~~~~~~~~~~~~~~~
2010-06-03 22:13:39 +04:00
QUANTIFIED/BETWEEN/COMPARISON/LIKE/NULL/EXISTS
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
QuantifiedExpression ::= ("ALL" | "ANY" | "SOME") "(" Subselect ")"
BetweenExpression ::= ArithmeticExpression ["NOT"] "BETWEEN" ArithmeticExpression "AND" ArithmeticExpression
ComparisonExpression ::= ArithmeticExpression ComparisonOperator ( QuantifiedExpression | ArithmeticExpression )
InExpression ::= StateFieldPathExpression ["NOT"] "IN" "(" (InParameter {"," InParameter}* | Subselect) ")"
LikeExpression ::= StringExpression ["NOT"] "LIKE" string ["ESCAPE" char]
NullComparisonExpression ::= (SingleValuedPathExpression | InputParameter) "IS" ["NOT"] "NULL"
ExistsExpression ::= ["NOT"] "EXISTS" "(" Subselect ")"
ComparisonOperator ::= "=" | "<" | "<=" | "<>" | ">" | ">=" | "!="
2010-11-01 23:16:12 +03:00
Functions
~~~~~~~~~
2010-06-03 22:13:39 +04:00
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-06-03 22:13:39 +04:00
2010-11-01 23:16:12 +03:00
FunctionDeclaration ::= FunctionsReturningStrings | FunctionsReturningNumerics | FunctionsReturningDateTime
2010-06-03 22:13:39 +04:00
FunctionsReturningNumerics ::=
"LENGTH" "(" StringPrimary ")" |
"LOCATE" "(" StringPrimary "," StringPrimary ["," SimpleArithmeticExpression]")" |
"ABS" "(" SimpleArithmeticExpression ")" | "SQRT" "(" SimpleArithmeticExpression ")" |
"MOD" "(" SimpleArithmeticExpression "," SimpleArithmeticExpression ")" |
"SIZE" "(" CollectionValuedPathExpression ")"
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
FunctionsReturningDateTime ::= "CURRENT_DATE" | "CURRENT_TIME" | "CURRENT_TIMESTAMP"
2010-11-01 23:16:12 +03:00
2010-06-03 22:13:39 +04:00
FunctionsReturningStrings ::=
"CONCAT" "(" StringPrimary "," StringPrimary ")" |
"SUBSTRING" "(" StringPrimary "," SimpleArithmeticExpression "," SimpleArithmeticExpression ")" |
"TRIM" "(" [["LEADING" | "TRAILING" | "BOTH"] [char] "FROM"] StringPrimary ")" |
"LOWER" "(" StringPrimary ")" |
2010-11-01 23:16:12 +03:00
"UPPER" "(" StringPrimary ")"