Doctrine


Doctrine\ORM\QueryBuilder
/Doctrine/ORM/QueryBuilder.php at line 38

Class QueryBuilder

QueryBuilder

public class QueryBuilder

This class is responsible for building DQL query strings via an object oriented PHP interface.

License:
http://www.opensource.org/licenses/lgpl-license.php LGPL
See Also:
www.doctrine-project.org
Since:
2.0
Version:
$Revision$
Author:
Guilherme Blanco
Jonathan Wage
Roman Borschel

Field Summary
final int

DELETE

final int

$SELECT

final int

STATE_CLEAN

final int

$STATE_DIRTY

final int

UPDATE

Constructor Summary

QueryBuilder(EntityManager em)

Initializes a new QueryBuilder that uses the given EntityManager.

Method Summary
QueryBuilder

add(string dqlPartName, string dqlPart, string append)

Add a single DQL query part to the array of parts

QueryBuilder

addGroupBy(string groupBy)

Add to the existing GROUP BY clause[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u') ->groupBy('u.last_login'); ->addGroupBy('u.created_at')

QueryBuilder

addOrderBy(string sort, string order)

Add to the existing ORDER BY clause

QueryBuilder

addSelect(mixed select)

Add to the SELECT statement[php] $qb = $em->createQueryBuilder() ->select('u') ->addSelect('p') ->from('User', 'u') ->leftJoin('u.Phonenumbers', 'p');

QueryBuilder

andHaving(mixed having)

Add to the existing HAVING clause with an AND

QueryBuilder

andWhere(mixed where)

Add a new WHERE statement with an AND[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u') ->where('u.username LIKE ?') ->andWhere('u.is_active = 1');

QueryBuilder

delete(string delete, string alias)

Construct a DQL DELETE query[php] $qb = $em->createQueryBuilder() ->delete('User', 'u') ->where('u.id = :user_id'); ->setParameter(':user_id', 1);

Expr

expr()

Gets an ExpressionBuilder used for object-oriented construction of query expressions.

QueryBuilder

from(string from, string alias)

Specify the FROM part when constructing a SELECT DQL query[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u')

string

getDQL()

Get the complete DQL string for this query builder instance[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u') echo $qb->getDql(); // SELECT u FROM User u

mixed

getDQLPart(string queryPartName)

Get a DQL part or parts by the part name

array

getDQLParts()

Get the full DQL parts array

EntityManager

getEntityManager()

Get the associated EntityManager for this query builder.

integer

getFirstResult()

Gets the position of the first result the query object was set to retrieve (the "offset").

integer

getMaxResults()

Gets the maximum number of results the query object was set to retrieve (the "limit").

mixed

getParameter(mixed key)

Gets a query parameter.

array

getParameters(mixed params)

Get all defined parameters

Query

getQuery()

Constructs a Query instance from the current configuration of the builder.

string

getRootAlias()

Get the root alias for the query.

integer

getState()

Get the state of this query builder instance[php] if ($qb->getState() == QueryBuilder::STATE_DIRTY) { echo 'Query builder is dirty'; } else { echo 'Query builder is clean'; }

integer

getType()

Get the type of the currently built query.

QueryBuilder

groupBy(string groupBy)

Set the GROUP BY clause[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u') ->groupBy('u.id');

QueryBuilder

having(mixed having)

Set the HAVING clause

QueryBuilder

innerJoin(string join, string alias, string conditionType, string condition)

Add an INNER JOIN to an associated class.

QueryBuilder

join(string join, string alias, string conditionType, string condition)

Add a INNER JOIN to an associated class.

QueryBuilder

leftJoin(string join, string alias, string conditionType, string condition)

Add a LEFT JOIN[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u') ->leftJoin('u.Phonenumbers', 'p', Expr\Join::WITH, 'p.is_primary = 1');

QueryBuilder

orHaving(mixed having)

Add to the existing HAVING clause with an OR

QueryBuilder

orWhere(mixed where)

Add a new WHERE statement with an OR[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u') ->where('u.id = 1') ->orWhere('u.id = 2');

QueryBuilder

orderBy(string sort, string order)

Set the ORDER BY clause

QueryBuilder

select(mixed select)

Set the SELECT statement[php] $qb = $em->createQueryBuilder() ->select('u', 'p') ->from('User', 'u') ->leftJoin('u.Phonenumbers', 'p');

QueryBuilder

set(string key, string value)

Add a SET statement for a DQL UPDATE query[php] $qb = $em->createQueryBuilder() ->update('User', 'u') ->set('u.password', md5('password')) ->where('u.id = ?');

QueryBuilder

setFirstResult(integer firstResult)

Sets the position of the first result to retrieve (the "offset").

QueryBuilder

setMaxResults(integer maxResults)

Sets the maximum number of results to retrieve (the "limit").

QueryBuilder

setParameter(string|integer key, mixed value)

Sets a query parameter.

QueryBuilder

setParameters(array params)

Sets a collection of query parameters.

QueryBuilder

update(string update, string alias)

Construct a DQL UPDATE query[php] $qb = $em->createQueryBuilder() ->update('User', 'u') ->set('u.password', md5('password')) ->where('u.id = ?');

QueryBuilder

where(mixed predicates)

Set and override any existing WHERE statements[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u') ->where('u.id = ?');You can optionally programatically build and/or expressions $qb = $em->createQueryBuilder();$or = $qb->expr()->orx(); $or->add($qb->expr()->eq('u.id', 1)); $or->add($qb->expr()->eq('u.id', 2));$qb->update('User', 'u') ->set('u.password', md5('password')) ->where($or);

Field Detail

/Doctrine/ORM/QueryBuilder.php at line 41

DELETE

public final int DELETE = 1

/Doctrine/ORM/QueryBuilder.php at line 40

SELECT

public final int $SELECT

/Doctrine/ORM/QueryBuilder.php at line 45

STATE_CLEAN

public final int STATE_CLEAN = 1

/Doctrine/ORM/QueryBuilder.php at line 44

STATE_DIRTY

public final int $STATE_DIRTY

/Doctrine/ORM/QueryBuilder.php at line 42

UPDATE

public final int UPDATE = 2

Constructor Detail

/Doctrine/ORM/QueryBuilder.php at line 101

QueryBuilder

public QueryBuilder(EntityManager em)

Initializes a new QueryBuilder that uses the given EntityManager.

Parameters:
em - The EntityManager to use.

Method Detail

/Doctrine/ORM/QueryBuilder.php at line 355

add

public QueryBuilder add(string dqlPartName, string dqlPart, string append)

Add a single DQL query part to the array of parts

Returns:
This QueryBuilder instance.

/Doctrine/ORM/QueryBuilder.php at line 689

addGroupBy

public QueryBuilder addGroupBy(string groupBy)

Add to the existing GROUP BY clause

[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u') ->groupBy('u.last_login'); ->addGroupBy('u.created_at')

Parameters:
groupBy - The GROUP BY clause
Returns:
$qb

/Doctrine/ORM/QueryBuilder.php at line 771

addOrderBy

public QueryBuilder addOrderBy(string sort, string order)

Add to the existing ORDER BY clause

Parameters:
sort - What to sort on
order - Optional: The order to sort the results.
Returns:
$qb

/Doctrine/ORM/QueryBuilder.php at line 408

addSelect

public QueryBuilder addSelect(mixed select)

Add to the SELECT statement

[php] $qb = $em->createQueryBuilder() ->select('u') ->addSelect('p') ->from('User', 'u') ->leftJoin('u.Phonenumbers', 'p');

Parameters:
select - String SELECT statement or SELECT Expr instance
Returns:
This QueryBuilder instance.

/Doctrine/ORM/QueryBuilder.php at line 715

andHaving

public QueryBuilder andHaving(mixed having)

Add to the existing HAVING clause with an AND

Returns:
$qb

/Doctrine/ORM/QueryBuilder.php at line 614

andWhere

public QueryBuilder andWhere(mixed where)

Add a new WHERE statement with an AND

[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u') ->where('u.username LIKE ?') ->andWhere('u.is_active = 1');

Parameters:
where - The WHERE statement
Returns:
$qb
See Also:
where()

/Doctrine/ORM/QueryBuilder.php at line 434

delete

public QueryBuilder delete(string delete, string alias)

Construct a DQL DELETE query

[php] $qb = $em->createQueryBuilder() ->delete('User', 'u') ->where('u.id = :user_id'); ->setParameter(':user_id', 1);

Parameters:
delete - The model to delete
alias - The alias of the model
Returns:
This QueryBuilder instance.

/Doctrine/ORM/QueryBuilder.php at line 118

expr

public Expr expr()

Gets an ExpressionBuilder used for object-oriented construction of query expressions. Intended for convenient inline usage. Example:

[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u') ->where($qb->expr()->eq('u.id', 1));


/Doctrine/ORM/QueryBuilder.php at line 481

from

public QueryBuilder from(string from, string alias)

Specify the FROM part when constructing a SELECT DQL query

[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u')

Parameters:
from - The class name.
alias - The alias of the class.
Returns:
This QueryBuilder instance.

/Doctrine/ORM/QueryBuilder.php at line 171

getDQL

public string getDQL()

Get the complete DQL string for this query builder instance

[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u') echo $qb->getDql(); // SELECT u FROM User u

Returns:
The DQL string

/Doctrine/ORM/QueryBuilder.php at line 782

getDQLPart

public mixed getDQLPart(string queryPartName)

Get a DQL part or parts by the part name

Returns:
$queryPart

/Doctrine/ORM/QueryBuilder.php at line 792

getDQLParts

public array getDQLParts()

Get the full DQL parts array

Returns:
$dqlParts

/Doctrine/ORM/QueryBuilder.php at line 138

getEntityManager

public EntityManager getEntityManager()

Get the associated EntityManager for this query builder.


/Doctrine/ORM/QueryBuilder.php at line 319

getFirstResult

public integer getFirstResult()

Gets the position of the first result the query object was set to retrieve (the "offset"). Returns NULL if setFirstResult was not applied to this query builder.

Returns:
The position of the first result.

/Doctrine/ORM/QueryBuilder.php at line 342

getMaxResults

public integer getMaxResults()

Gets the maximum number of results the query object was set to retrieve (the "limit"). Returns NULL if setMaxResults was not applied to this query builder.

Returns:
Maximum number of results.

/Doctrine/ORM/QueryBuilder.php at line 296

getParameter

public mixed getParameter(mixed key)

Gets a query parameter.

Parameters:
key - The key (index or name) of the bound parameter.
Returns:
The value of the bound parameter.

/Doctrine/ORM/QueryBuilder.php at line 285

getParameters

public array getParameters(mixed params)

Get all defined parameters

Returns:
Defined parameters

/Doctrine/ORM/QueryBuilder.php at line 212

getQuery

public Query getQuery()

Constructs a Query instance from the current configuration of the builder.

[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u'); $q = $qb->getQuery(); $results = $q->execute();


/Doctrine/ORM/QueryBuilder.php at line 233

getRootAlias

public string getRootAlias()

Get the root alias for the query. This is the first entity alias involved in the construction of the query

[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u');

echo $qb->getRootAlias(); // u

Returns:
$rootAlias

/Doctrine/ORM/QueryBuilder.php at line 155

getState

public integer getState()

Get the state of this query builder instance

[php] if ($qb->getState() == QueryBuilder::STATE_DIRTY) { echo 'Query builder is dirty'; } else { echo 'Query builder is clean'; }


/Doctrine/ORM/QueryBuilder.php at line 128

getType

public integer getType()

Get the type of the currently built query.


/Doctrine/ORM/QueryBuilder.php at line 670

groupBy

public QueryBuilder groupBy(string groupBy)

Set the GROUP BY clause

[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u') ->groupBy('u.id');

Parameters:
groupBy - The GROUP BY clause
Returns:
$qb

/Doctrine/ORM/QueryBuilder.php at line 700

having

public QueryBuilder having(mixed having)

Set the HAVING clause

Returns:
$qb

/Doctrine/ORM/QueryBuilder.php at line 521

innerJoin

public QueryBuilder innerJoin(string join, string alias, string conditionType, string condition)

Add an INNER JOIN to an associated class.

[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u') ->innerJoin('u.Phonenumbers', 'p', Expr\Join::WITH, 'p.is_primary = 1');

Parameters:
join - The relationship to join
alias - The alias of the join
conditionType - The condition type constant. Either ON or WITH.
condition - The condition for the join
Returns:
This QueryBuilder instance.

/Doctrine/ORM/QueryBuilder.php at line 501

join

public QueryBuilder join(string join, string alias, string conditionType, string condition)

Add a INNER JOIN to an associated class.

[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u') ->innerJoin('u.Phonenumbers', 'p', Expr\Join::WITH, 'p.is_primary = 1');

Parameters:
join - The relationship to join
alias - The alias of the join
conditionType - The condition type constant. Either ON or WITH.
condition - The condition for the join
Returns:
This QueryBuilder instance.

/Doctrine/ORM/QueryBuilder.php at line 543

leftJoin

public QueryBuilder leftJoin(string join, string alias, string conditionType, string condition)

Add a LEFT JOIN

[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u') ->leftJoin('u.Phonenumbers', 'p', Expr\Join::WITH, 'p.is_primary = 1');

Parameters:
join - The relationship to join
alias - The alias of the join
conditionType - The condition type constant. Either ON or WITH.
condition - The condition for the join
Returns:
$qb

/Doctrine/ORM/QueryBuilder.php at line 736

orHaving

public QueryBuilder orHaving(mixed having)

Add to the existing HAVING clause with an OR

Returns:
$qb

/Doctrine/ORM/QueryBuilder.php at line 643

orWhere

public QueryBuilder orWhere(mixed where)

Add a new WHERE statement with an OR

[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u') ->where('u.id = 1') ->orWhere('u.id = 2');

Parameters:
where - The WHERE statement
Returns:
$qb
See Also:
where()

/Doctrine/ORM/QueryBuilder.php at line 758

orderBy

public QueryBuilder orderBy(string sort, string order)

Set the ORDER BY clause

Parameters:
sort - What to sort on
order - Optional: The order to sort the results.
Returns:
$qb

/Doctrine/ORM/QueryBuilder.php at line 382

select

public QueryBuilder select(mixed select)

Set the SELECT statement

[php] $qb = $em->createQueryBuilder() ->select('u', 'p') ->from('User', 'u') ->leftJoin('u.Phonenumbers', 'p');

Parameters:
select - String SELECT statement or SELECT Expr instance
Returns:
This QueryBuilder instance.

/Doctrine/ORM/QueryBuilder.php at line 563

set

public QueryBuilder set(string key, string value)

Add a SET statement for a DQL UPDATE query

[php] $qb = $em->createQueryBuilder() ->update('User', 'u') ->set('u.password', md5('password')) ->where('u.id = ?');

Parameters:
key - The key/field to set
value - The value, expression, placeholder, etc. to use in the SET
Returns:
$qb

/Doctrine/ORM/QueryBuilder.php at line 307

setFirstResult

public QueryBuilder setFirstResult(integer firstResult)

Sets the position of the first result to retrieve (the "offset").

Parameters:
firstResult - The first result to return.
Returns:
This QueryBuilder instance.

/Doctrine/ORM/QueryBuilder.php at line 330

setMaxResults

public QueryBuilder setMaxResults(integer maxResults)

Sets the maximum number of results to retrieve (the "limit").

Returns:
This QueryBuilder instance.

/Doctrine/ORM/QueryBuilder.php at line 252

setParameter

public QueryBuilder setParameter(string|integer key, mixed value)

Sets a query parameter.

[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u') ->where('u.id = :user_id') ->setParameter(':user_id', 1);

Parameters:
key - The parameter position or name.
value - The parameter value.
Returns:
This QueryBuilder instance.

/Doctrine/ORM/QueryBuilder.php at line 274

setParameters

public QueryBuilder setParameters(array params)

Sets a collection of query parameters.

[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u') ->where('u.id = :user_id1 OR u.id = :user_id2') ->setParameters(array( ':user_id1' => 1, ':user_id2' => 2 ));

Returns:
This QueryBuilder instance.

/Doctrine/ORM/QueryBuilder.php at line 458

update

public QueryBuilder update(string update, string alias)

Construct a DQL UPDATE query

[php] $qb = $em->createQueryBuilder() ->update('User', 'u') ->set('u.password', md5('password')) ->where('u.id = ?');

Parameters:
update - The model to update
alias - The alias of the model
Returns:
This QueryBuilder instance.

/Doctrine/ORM/QueryBuilder.php at line 591

where

public QueryBuilder where(mixed predicates)

Set and override any existing WHERE statements

[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u') ->where('u.id = ?');

You can optionally programatically build and/or expressions $qb = $em->createQueryBuilder();

$or = $qb->expr()->orx(); $or->add($qb->expr()->eq('u.id', 1)); $or->add($qb->expr()->eq('u.id', 2));

$qb->update('User', 'u') ->set('u.password', md5('password')) ->where($or);

Parameters:
predicates - The predicates.

Doctrine