QueryBuilder
public class QueryBuilder
www.doctrine-project.org
Field Summary | |
---|---|
final int | |
final int | |
final int | |
final int | |
final int |
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 | Get the full DQL parts array |
EntityManager | Get the associated EntityManager for this query builder. |
integer | Gets the position of the first result the query object was set to retrieve (the "offset"). |
integer | 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 | 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); |
public final int DELETE = 1
public final int $SELECT
public final int STATE_CLEAN = 1
public final int $STATE_DIRTY
public final int UPDATE = 2
public QueryBuilder(EntityManager em)
Initializes a new QueryBuilder that uses the given EntityManager.
public QueryBuilder add(string dqlPartName, string dqlPart, string append)
Add a single DQL query part to the array of parts
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')
public QueryBuilder addOrderBy(string sort, string order)
Add to the existing ORDER BY clause
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');
public QueryBuilder andHaving(mixed having)
Add to the existing HAVING clause with an AND
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');
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);
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));
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')
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
public mixed getDQLPart(string queryPartName)
Get a DQL part or parts by the part name
public array getDQLParts()
Get the full DQL parts array
public EntityManager getEntityManager()
Get the associated EntityManager for this query builder.
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.
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.
public mixed getParameter(mixed key)
Gets a query parameter.
public array getParameters(mixed params)
Get all defined parameters
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();
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
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'; }
public integer getType()
Get the type of the currently built query.
public QueryBuilder groupBy(string groupBy)
Set the GROUP BY clause
[php] $qb = $em->createQueryBuilder() ->select('u') ->from('User', 'u') ->groupBy('u.id');
public QueryBuilder having(mixed having)
Set the HAVING clause
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');
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');
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');
public QueryBuilder orHaving(mixed having)
Add to the existing HAVING clause with an OR
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');
public QueryBuilder orderBy(string sort, string order)
Set the ORDER BY clause
public QueryBuilder select(mixed select)
Set the SELECT statement
[php] $qb = $em->createQueryBuilder() ->select('u', 'p') ->from('User', 'u') ->leftJoin('u.Phonenumbers', 'p');
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 = ?');
public QueryBuilder setFirstResult(integer firstResult)
Sets the position of the first result to retrieve (the "offset").
public QueryBuilder setMaxResults(integer maxResults)
Sets the maximum number of results to retrieve (the "limit").
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);
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 ));
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 = ?');
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);
This class is responsible for building DQL query strings via an object oriented PHP interface.