| Methods |
public
|
__construct(EntityManagerInterface $em)
Initializes a new <tt>QueryBuilder</tt> that uses the given <tt>EntityManager</tt>.
Initializes a new QueryBuilder that uses the given EntityManager.
Parameters
| $em |
The EntityManager to use.
|
|
#
|
public
|
expr(): Expr
Gets an ExpressionBuilder used for object-oriented construction of query expressions.
This producer method is intended…
Gets an ExpressionBuilder used for object-oriented construction of query expressions.
This producer method is intended for convenient inline usage. Example:
$qb = $em->createQueryBuilder();
$qb
->select('u')
->from('User', 'u')
->where($qb->expr()->eq('u.id', 1));
For more complex expression construction, consider storing the expression
builder object in a local variable.
|
#
|
public
|
setCacheable(bool $cacheable): $this
Enable/disable second level query (result) caching for this query.
Enable/disable second level query (result) caching for this query.
|
#
|
public
|
isCacheable(): bool
Are the query results enabled for second level cache?
Are the query results enabled for second level cache?
|
#
|
public
|
setCacheRegion(string $cacheRegion): $this
|
#
|
public
|
getCacheRegion(): string|null
Obtain the name of the second level query cache region in which query results will be stored
Obtain the name of the second level query cache region in which query results will be stored
Returns
The cache region name; NULL indicates the default region.
|
#
|
public
|
getLifetime(): int
|
#
|
public
|
setLifetime(int $lifetime): $this
Sets the life-time for this query into second level cache.
Sets the life-time for this query into second level cache.
|
#
|
public
|
getCacheMode(): int|null
|
#
|
public
|
setCacheMode(Cache::MODE_* $cacheMode): $this
|
#
|
public
|
getType(): int
Gets the type of the currently built query.
Gets the type of the currently built query.
Deprecated
If necessary, track the type of the query being built outside of the builder.
|
#
|
public
|
getEntityManager(): EntityManagerInterface
Gets the associated EntityManager for this query builder.
Gets the associated EntityManager for this query builder.
|
#
|
public
|
getState(): int
Gets the state of this query builder instance.
Gets the state of this query builder instance.
Deprecated
The builder state is an internal concern.
Returns
Either QueryBuilder::STATE_DIRTY or QueryBuilder::STATE_CLEAN.
|
#
|
public
|
getDQL(): string
Gets the complete DQL string formed by the current specifications of this QueryBuilder.
Gets the complete DQL string formed by the current specifications of this QueryBuilder.
$qb = $em->createQueryBuilder()
->select('u')
->from('User', 'u');
echo $qb->getDql(); // SELECT u FROM User u
Returns
|
#
|
public
|
getQuery(): Query
Constructs a Query instance from the current specifications of the builder.
Constructs a Query instance from the current specifications of the builder.
$qb = $em->createQueryBuilder()
->select('u')
->from('User', 'u');
$q = $qb->getQuery();
$results = $q->execute();
|
#
|
private
|
findRootAlias(string $alias, string $parentAlias): string
Finds the root entity alias of the joined entity.
Finds the root entity alias of the joined entity.
Parameters
| $alias |
The alias of the new join entity
|
| $parentAlias |
The parent entity alias of the join relationship
|
|
#
|
public
|
getRootAlias(): string
Gets the FIRST root alias of the query. This is the first entity alias involved
in the construction of the query.
Gets the FIRST root alias of the query. This is the first entity alias involved
in the construction of the query.
$qb = $em->createQueryBuilder()
->select('u')
->from('User', 'u');
echo $qb->getRootAlias(); // u
Deprecated
Please use $qb->getRootAliases() instead.
Throws
|
#
|
public
|
getRootAliases(): string[]
Gets the root aliases of the query. This is the entity aliases involved
in the construction of the query.
Gets the root aliases of the query. This is the entity aliases involved
in the construction of the query.
$qb = $em->createQueryBuilder()
->select('u')
->from('User', 'u');
$qb->getRootAliases(); // array('u')
|
#
|
public
|
getAllAliases(): string[]
Gets all the aliases that have been used in the query.
Including all select root aliases and join aliases
Gets all the aliases that have been used in the query.
Including all select root aliases and join aliases
$qb = $em->createQueryBuilder()
->select('u')
->from('User', 'u')
->join('u.articles','a');
$qb->getAllAliases(); // array('u','a')
|
#
|
public
|
getRootEntities(): string[]
Gets the root entities of the query. This is the entity aliases involved
in the construction of the query.
Gets the root entities of the query. This is the entity aliases involved
in the construction of the query.
$qb = $em->createQueryBuilder()
->select('u')
->from('User', 'u');
$qb->getRootEntities(); // array('User')
|
#
|
public
|
setParameter(string|int $key, mixed $value, string|int|null $type = null): $this
Sets a query parameter for the query being constructed.
Sets a query parameter for the query being constructed.
$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.
|
| $type |
ParameterType::* or \Doctrine\DBAL\Types\Type::* constant
|
|
#
|
public
|
setParameters(ArrayCollection<int, Parameter>|mixed[] $parameters): $this
Sets a collection of query parameters for the query being constructed.
Sets a collection of query parameters for the query being constructed.
$qb = $em->createQueryBuilder()
->select('u')
->from('User', 'u')
->where('u.id = :user_id1 OR u.id = :user_id2')
->setParameters(new ArrayCollection(array(
new Parameter('user_id1', 1),
new Parameter('user_id2', 2)
)));
|
#
|
public
|
getParameters(): ArrayCollection
Gets all defined query parameters for the query being constructed.
Gets all defined query parameters for the query being constructed.
Returns
The currently defined query parameters.
|
#
|
public
|
getParameter(string|int $key): Parameter|null
Gets a (previously set) query parameter of the query being constructed.
Gets a (previously set) query parameter of the query being constructed.
Parameters
| $key |
The key (index or name) of the bound parameter.
|
Returns
The value of the bound parameter.
|
#
|
public
|
setFirstResult(int|null $firstResult): $this
Sets the position of the first result to retrieve (the "offset").
Sets the position of the first result to retrieve (the "offset").
Parameters
| $firstResult |
The first result to return.
|
|
#
|
public
|
getFirstResult(): int|null
Gets the position of the first result the query object was set to retrieve (the "offset").
Returns NULL if {@link…
Gets the position of the first result the query object was set to retrieve (the "offset").
Returns NULL if {@link setFirstResult} was not applied to this QueryBuilder.
Returns
The position of the first result.
|
#
|
public
|
setMaxResults(int|null $maxResults): $this
Sets the maximum number of results to retrieve (the "limit").
Sets the maximum number of results to retrieve (the "limit").
Parameters
| $maxResults |
The maximum number of results to retrieve.
|
|
#
|
public
|
getMaxResults(): int|null
Gets the maximum number of results the query object was set to retrieve (the "limit").
Returns NULL if {@link…
Gets the maximum number of results the query object was set to retrieve (the "limit").
Returns NULL if {@link setMaxResults} was not applied to this query builder.
Returns
Maximum number of results.
|
#
|
public
|
add(
string $dqlPartName,
string|object|list<string>|array{join: array<int|string, object>} $dqlPart,
bool $append = false,
): $this
Either appends to or replaces a single, generic query part.
Either appends to or replaces a single, generic query part.
The available parts are: 'select', 'from', 'join', 'set', 'where',
'groupBy', 'having' and 'orderBy'.
Parameters
| $dqlPartName |
The DQL part name.
|
| $append |
Whether to append (true) or replace (false).
|
|
#
|
public
|
select(mixed $select = null): $this
Specifies an item that is to be returned in the query result.
Replaces any previously specified selections, if any.
Specifies an item that is to be returned in the query result.
Replaces any previously specified selections, if any.
$qb = $em->createQueryBuilder()
->select('u', 'p')
->from('User', 'u')
->leftJoin('u.Phonenumbers', 'p');
Parameters
| $select |
The selection expressions.
|
|
#
|
public
|
distinct(bool $flag = true): $this
Adds a DISTINCT flag to this query.
Adds a DISTINCT flag to this query.
$qb = $em->createQueryBuilder()
->select('u')
->distinct()
->from('User', 'u');
|
#
|
public
|
addSelect(mixed $select = null): $this
Adds an item that is to be returned in the query result.
Adds an item that is to be returned in the query result.
$qb = $em->createQueryBuilder()
->select('u')
->addSelect('p')
->from('User', 'u')
->leftJoin('u.Phonenumbers', 'p');
Parameters
| $select |
The selection expression.
|
|
#
|
public
|
delete(string|null $delete = null, string|null $alias = null): $this
Turns the query being built into a bulk delete query that ranges over
a certain entity type.
Turns the query being built into a bulk delete query that ranges over
a certain entity type.
$qb = $em->createQueryBuilder()
->delete('User', 'u')
->where('u.id = :user_id')
->setParameter('user_id', 1);
Parameters
| $delete |
The class/type whose instances are subject to the deletion.
|
| $alias |
The class/type alias used in the constructed query.
|
|
#
|
public
|
update(string|null $update = null, string|null $alias = null): $this
Turns the query being built into a bulk update query that ranges over
a certain entity type.
Turns the query being built into a bulk update query that ranges over
a certain entity type.
$qb = $em->createQueryBuilder()
->update('User', 'u')
->set('u.password', '?1')
->where('u.id = ?2');
Parameters
| $update |
The class/type whose instances are subject to the update.
|
| $alias |
The class/type alias used in the constructed query.
|
|
#
|
public
|
from(string $from, string $alias, string|null $indexBy = null): $this
Creates and adds a query root corresponding to the entity identified by the given alias,
forming a cartesian product…
Creates and adds a query root corresponding to the entity identified by the given alias,
forming a cartesian product with any existing query roots.
$qb = $em->createQueryBuilder()
->select('u')
->from('User', 'u');
Parameters
| $from |
The class name.
|
| $alias |
The alias of the class.
|
| $indexBy |
The index for the from.
|
|
#
|
public
|
indexBy(string $alias, string $indexBy): $this
Updates a query root corresponding to an entity setting its index by. This method is intended to be used with…
Updates a query root corresponding to an entity setting its index by. This method is intended to be used with
EntityRepository->createQueryBuilder(), which creates the initial FROM clause and do not allow you to update it
setting an index by.
$qb = $userRepository->createQueryBuilder('u')
->indexBy('u', 'u.id');
// Is equivalent to...
$qb = $em->createQueryBuilder()
->select('u')
->from('User', 'u', 'u.id');
Parameters
| $alias |
The root alias of the class.
|
| $indexBy |
The index for the from.
|
Throws
|
#
|
public
|
join(
string $join,
string $alias,
Join::ON|Join::WITH|null $conditionType = null,
string|Comparison|Composite|Func|null $condition = null,
string|null $indexBy = null,
): $this
Creates and adds a join over an entity association to the query.
Creates and adds a join over an entity association to the query.
The entities in the joined association will be fetched as part of the query
result if the alias used for the joined association is placed in the select
expressions.
$qb = $em->createQueryBuilder()
->select('u')
->from('User', 'u')
->join('u.Phonenumbers', 'p', Expr\Join::WITH, 'p.is_primary = 1');
Parameters
| $join |
The relationship to join.
|
| $alias |
The alias of the join.
|
| $condition |
The condition for the join.
|
| $indexBy |
The index for the join.
|
|
#
|
public
|
innerJoin(
string $join,
string $alias,
Join::ON|Join::WITH|null $conditionType = null,
string|Comparison|Composite|Func|null $condition = null,
string|null $indexBy = null,
): $this
Creates and adds a join over an entity association to the query.
Creates and adds a join over an entity association to the query.
The entities in the joined association will be fetched as part of the query
result if the alias used for the joined association is placed in the select
expressions.
[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.
|
| $condition |
The condition for the join.
|
| $indexBy |
The index for the join.
|
|
#
|
public
|
leftJoin(
string $join,
string $alias,
Join::ON|Join::WITH|null $conditionType = null,
string|Comparison|Composite|Func|null $condition = null,
string|null $indexBy = null,
): $this
Creates and adds a left join over an entity association to the query.
Creates and adds a left join over an entity association to the query.
The entities in the joined association will be fetched as part of the query
result if the alias used for the joined association is placed in the select
expressions.
$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.
|
| $condition |
The condition for the join.
|
| $indexBy |
The index for the join.
|
|
#
|
public
|
set(string $key, mixed $value): $this
Sets a new value for a field in a bulk update query.
Sets a new value for a field in a bulk update query.
$qb = $em->createQueryBuilder()
->update('User', 'u')
->set('u.password', '?1')
->where('u.id = ?2');
Parameters
| $key |
The key/field to set.
|
| $value |
The value, expression, placeholder, etc.
|
|
#
|
public
|
where(mixed $predicates): $this
Specifies one or more restrictions to the query result.
Replaces any previously specified restrictions, if any.
Specifies one or more restrictions to the query result.
Replaces any previously specified restrictions, if any.
$qb = $em->createQueryBuilder()
->select('u')
->from('User', 'u')
->where('u.id = ?');
// You can optionally programmatically 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', '?')
->where($or);
Parameters
| $predicates |
The restriction predicates.
|
|
#
|
public
|
andWhere(): $this
Adds one or more restrictions to the query results, forming a logical
conjunction with any previously specified…
Adds one or more restrictions to the query results, forming a logical
conjunction with any previously specified restrictions.
$qb = $em->createQueryBuilder()
->select('u')
->from('User', 'u')
->where('u.username LIKE ?')
->andWhere('u.is_active = 1');
|
#
|
public
|
orWhere(): $this
Adds one or more restrictions to the query results, forming a logical
disjunction with any previously specified…
Adds one or more restrictions to the query results, forming a logical
disjunction with any previously specified restrictions.
$qb = $em->createQueryBuilder()
->select('u')
->from('User', 'u')
->where('u.id = 1')
->orWhere('u.id = 2');
|
#
|
public
|
groupBy(string $groupBy): $this
Specifies a grouping over the results of the query.
Replaces any previously specified groupings, if any.
Specifies a grouping over the results of the query.
Replaces any previously specified groupings, if any.
$qb = $em->createQueryBuilder()
->select('u')
->from('User', 'u')
->groupBy('u.id');
Parameters
| $groupBy |
The grouping expression.
|
|
#
|
public
|
addGroupBy(string $groupBy): $this
Adds a grouping expression to the query.
Adds a grouping expression to the query.
$qb = $em->createQueryBuilder()
->select('u')
->from('User', 'u')
->groupBy('u.lastLogin')
->addGroupBy('u.createdAt');
Parameters
| $groupBy |
The grouping expression.
|
|
#
|
public
|
having(mixed $having): $this
Specifies a restriction over the groups of the query.
Replaces any previous having restrictions, if any.
Specifies a restriction over the groups of the query.
Replaces any previous having restrictions, if any.
Parameters
| $having |
The restriction over the groups.
|
|
#
|
public
|
andHaving(mixed $having): $this
Adds a restriction over the groups of the query, forming a logical
conjunction with any existing having restrictions.
Adds a restriction over the groups of the query, forming a logical
conjunction with any existing having restrictions.
Parameters
| $having |
The restriction to append.
|
|
#
|
public
|
orHaving(mixed $having): $this
Adds a restriction over the groups of the query, forming a logical
disjunction with any existing having restrictions.
Adds a restriction over the groups of the query, forming a logical
disjunction with any existing having restrictions.
Parameters
| $having |
The restriction to add.
|
|
#
|
public
|
orderBy(string|OrderBy $sort, string|null $order = null): $this
Specifies an ordering for the query results.
Replaces any previously specified orderings, if any.
Specifies an ordering for the query results.
Replaces any previously specified orderings, if any.
Parameters
| $sort |
The ordering expression.
|
| $order |
The ordering direction.
|
|
#
|
public
|
addOrderBy(string|OrderBy $sort, string|null $order = null): $this
Adds an ordering to the query results.
Adds an ordering to the query results.
Parameters
| $sort |
The ordering expression.
|
| $order |
The ordering direction.
|
|
#
|
public
|
addCriteria(Criteria $criteria): $this
Adds criteria to the query.
Adds criteria to the query.
Adds where expressions with AND operator.
Adds orderings.
Overrides firstResult and maxResults if they're set.
Throws
|
#
|
public
|
getDQLPart(string $queryPartName): mixed
Gets a query part by its name.
Gets a query part by its name.
Returns
|
#
|
public
|
getDQLParts()
Gets all query parts.
|
#
|
private
|
getDQLForDelete(): string
|
#
|
private
|
getDQLForUpdate(): string
|
#
|
private
|
getDQLForSelect(): string
|
#
|
private
|
getReducedDQLQueryPart(string $queryPartName, array<string, mixed> $options = []): string
|
#
|
public
|
resetDQLParts(list<string>|null $parts = null): $this
Resets DQL parts.
|
#
|
public
|
resetDQLPart(string $part): $this
Resets single DQL part.
|
#
|
public
|
__toString(): string
Gets a string representation of this QueryBuilder which corresponds to
the final DQL query being constructed.
Gets a string representation of this QueryBuilder which corresponds to
the final DQL query being constructed.
Returns
The string representation of this QueryBuilder.
|
#
|
public
|
__clone(): void
Deep clones all expression objects in the DQL parts.
Deep clones all expression objects in the DQL parts.
|
#
|