Method | Description | |
---|---|---|
__clone ( ) : void | Deep clone of all expression objects in the SQL parts. | |
__construct ( Doctrine\DBAL\Connection $connection ) | Initializes a new QueryBuilder. | |
__toString ( ) : string | Gets a string representation of this QueryBuilder which corresponds to the final SQL query being constructed. | |
add ( string $sqlPartName, string $sqlPart, boolean $append = false ) | Either appends to or replaces a single, generic query part. | |
addGroupBy ( mixed $groupBy ) | Adds a grouping expression to the query. | |
addOrderBy ( string $sort, string $order = null ) | Adds an ordering to the query results. | |
addSelect ( mixed $select = null ) | Adds an item that is to be returned in the query result. | |
andHaving ( mixed $having ) | Adds a restriction over the groups of the query, forming a logical conjunction with any existing having restrictions. | |
andWhere ( mixed $where ) | Adds one or more restrictions to the query results, forming a logical conjunction with any previously specified restrictions. | |
createNamedParameter ( mixed $value, mixed $type = PDO::PARAM_STR, string $placeHolder = null ) : string | Creates a new named parameter and bind the value $value to it. | |
createPositionalParameter ( mixed $value, integer $type = PDO::PARAM_STR ) : string | Creates a new positional parameter and bind the given value to it. | |
delete ( string $delete = null, string $alias = null ) | Turns the query being built into a bulk delete query that ranges over a certain table. | |
execute ( ) : Doctrine\DBAL\Driver\Statement | integer | Executes this query using the bound parameters and their types. | |
expr ( ) : Doctrine\DBAL\Query\Expression\ExpressionBuilder | Gets an ExpressionBuilder used for object-oriented construction of query expressions. | |
from ( string $from, string | null $alias = null ) | Creates and adds a query root corresponding to the table identified by the given alias, forming a cartesian product with any existing query roots. | |
getConnection ( ) : Doctrine\DBAL\Connection | Gets the associated DBAL Connection for this query builder. | |
getFirstResult ( ) : integer | Gets the position of the first result the query object was set to retrieve (the "offset"). | |
getMaxResults ( ) : integer | Gets the maximum number of results the query object was set to retrieve (the "limit"). | |
getParameter ( mixed $key ) : mixed | Gets a (previously set) query parameter of the query being constructed. | |
getParameterType ( mixed $key ) : mixed | Gets a (previously set) query parameter type of the query being constructed. | |
getParameterTypes ( ) : array | Gets all defined query parameter types for the query being constructed indexed by parameter index or name. | |
getParameters ( ) : array | Gets all defined query parameters for the query being constructed indexed by parameter index or name. | |
getQueryPart ( string $queryPartName ) : mixed | Gets a query part by its name. | |
getQueryParts ( ) : array | Gets all query parts. | |
getSQL ( ) : string | Gets the complete SQL string formed by the current specifications of this QueryBuilder. | |
getState ( ) : integer | Gets the state of this query builder instance. | |
getType ( ) : integer | Gets the type of the currently built query. | |
groupBy ( mixed $groupBy ) | Specifies a grouping over the results of the query. | |
having ( mixed $having ) | Specifies a restriction over the groups of the query. | |
innerJoin ( string $fromAlias, string $join, string $alias, string $condition = null ) | Creates and adds a join to the query. | |
insert ( string $insert = null ) | Turns the query being built into an insert query that inserts into a certain table | |
join ( string $fromAlias, string $join, string $alias, string $condition = null ) | Creates and adds a join to the query. | |
leftJoin ( string $fromAlias, string $join, string $alias, string $condition = null ) | Creates and adds a left join to the query. | |
orHaving ( mixed $having ) | Adds a restriction over the groups of the query, forming a logical disjunction with any existing having restrictions. | |
orWhere ( mixed $where ) | Adds one or more restrictions to the query results, forming a logical disjunction with any previously specified restrictions. | |
orderBy ( string $sort, string $order = null ) | Specifies an ordering for the query results. | |
resetQueryPart ( string $queryPartName ) | Resets a single SQL part. | |
resetQueryParts ( array | null $queryPartNames = null ) | Resets SQL parts. | |
rightJoin ( string $fromAlias, string $join, string $alias, string $condition = null ) | Creates and adds a right join to the query. | |
select ( mixed $select = null ) | Specifies an item that is to be returned in the query result. | |
set ( string $key, string $value ) | Sets a new value for a column in a bulk update query. | |
setFirstResult ( integer $firstResult ) | Sets the position of the first result to retrieve (the "offset"). | |
setMaxResults ( integer $maxResults ) | Sets the maximum number of results to retrieve (the "limit"). | |
setParameter ( string | integer $key, mixed $value, string | null $type = null ) | Sets a query parameter for the query being constructed. | |
setParameters ( array $params, array $types = [] ) | Sets a collection of query parameters for the query being constructed. | |
setValue ( string $column, string $value ) | Sets a value for a column in an insert query. | |
update ( string $update = null, string $alias = null ) | Turns the query being built into a bulk update query that ranges over a certain table | |
values ( array $values ) | Specifies values for an insert query indexed by column names. | |
where ( mixed $predicates ) | Specifies one or more restrictions to the query result. |
Method | Description | |
---|---|---|
getFromClauses ( ) : string[] | ||
getSQLForDelete ( ) : string | Converts this instance into a DELETE string in SQL. | |
getSQLForInsert ( ) : string | Converts this instance into an INSERT string in SQL. | |
getSQLForJoins ( string $fromAlias, array &$knownAliases ) : string | ||
getSQLForSelect ( ) : string | ||
getSQLForUpdate ( ) : string | Converts this instance into an UPDATE string in SQL. | |
isLimitQuery ( ) : boolean | ||
verifyAllAliasesAreKnown ( array $knownAliases ) |
public __construct ( Doctrine\DBAL\Connection $connection ) | ||
$connection | Doctrine\DBAL\Connection | The DBAL Connection. |
public __toString ( ) : string | ||
return | string | The string representation of this QueryBuilder. |
$qb = $conn->createQueryBuilder()
->select('u.name')
->from('users', 'u')
->groupBy('u.lastLogin');
->addGroupBy('u.createdAt')
public addGroupBy ( mixed $groupBy ) | ||
$groupBy | mixed | The grouping expression. |
public addOrderBy ( string $sort, string $order = null ) | ||
$sort | string | The ordering expression. |
$order | string | The ordering direction. |
$qb = $conn->createQueryBuilder()
->select('u.id')
->addSelect('p.id')
->from('users', 'u')
->leftJoin('u', 'phonenumbers', 'u.id = p.user_id');
$qb = $conn->createQueryBuilder()
->select('u')
->from('users', 'u')
->where('u.username LIKE ?')
->andWhere('u.is_active = 1');
$value = 2;
$q->eq( 'id', $q->bindValue( $value ) );
$stmt = $q->executeQuery(); // executed with 'id = 2'
$qb = $conn->createQueryBuilder();
$qb->select('u.*')
->from('users', 'u')
->where('u.username = ' . $qb->createPositionalParameter('Foo', PDO::PARAM_STR))
->orWhere('u.username = ' . $qb->createPositionalParameter('Bar', PDO::PARAM_STR))
$qb = $conn->createQueryBuilder()
->delete('users', 'u')
->where('u.id = :user_id');
->setParameter(':user_id', 1);
$qb = $conn->createQueryBuilder()
->select('u')
->from('users', 'u')
->where($qb->expr()->eq('u.id', 1));
For more complex expression construction, consider storing the expression
builder object in a local variable. public expr ( ) : Doctrine\DBAL\Query\Expression\ExpressionBuilder | ||
return | Doctrine\DBAL\Query\Expression\ExpressionBuilder |
$qb = $conn->createQueryBuilder()
->select('u.id')
->from('users', 'u')
public getConnection ( ) : Doctrine\DBAL\Connection | ||
return | Doctrine\DBAL\Connection |
public getFirstResult ( ) : integer | ||
return | integer | The position of the first result. |
public getMaxResults ( ) : integer | ||
return | integer | The maximum number of results. |
public getParameter ( mixed $key ) : mixed | ||
$key | mixed | The key (index or name) of the bound parameter. |
return | mixed | The value of the bound parameter. |
public getParameterType ( mixed $key ) : mixed | ||
$key | mixed | The key (index or name) of the bound parameter type. |
return | mixed | The value of the bound parameter type. |
public getParameterTypes ( ) : array | ||
return | array | The currently defined query parameter types indexed by parameter index or name. |
public getParameters ( ) : array | ||
return | array | The currently defined query parameters indexed by parameter index or name. |
public getQueryPart ( string $queryPartName ) : mixed | ||
$queryPartName | string | |
return | mixed |
$qb = $em->createQueryBuilder()
->select('u')
->from('User', 'u')
echo $qb->getSQL(); // SELECT u FROM User u
$qb = $conn->createQueryBuilder()
->select('u.name')
->from('users', 'u')
->groupBy('u.id');
$qb = $conn->createQueryBuilder()
->select('u.name')
->from('users', 'u')
->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
$qb = $conn->createQueryBuilder()
->insert('users')
->values(
array(
'name' => '?',
'password' => '?'
)
);
$qb = $conn->createQueryBuilder()
->select('u.name')
->from('users', 'u')
->join('u', 'phonenumbers', 'p', 'p.is_primary = 1');
$qb = $conn->createQueryBuilder()
->select('u.name')
->from('users', 'u')
->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
$qb = $em->createQueryBuilder()
->select('u.name')
->from('users', 'u')
->where('u.id = 1')
->orWhere('u.id = 2');
public resetQueryPart ( string $queryPartName ) | ||
$queryPartName | string |
public resetQueryParts ( array | null $queryPartNames = null ) | ||
$queryPartNames | array | null |
$qb = $conn->createQueryBuilder()
->select('u.name')
->from('users', 'u')
->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
$qb = $conn->createQueryBuilder()
->select('u.id', 'p.id')
->from('users', 'u')
->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id');
$qb = $conn->createQueryBuilder()
->update('users', 'u')
->set('u.password', md5('password'))
->where('u.id = ?');
public setFirstResult ( integer $firstResult ) | ||
$firstResult | integer | The first result to return. |
public setMaxResults ( integer $maxResults ) | ||
$maxResults | integer | The maximum number of results to retrieve. |
$qb = $conn->createQueryBuilder()
->select('u')
->from('users', 'u')
->where('u.id = :user_id')
->setParameter(':user_id', 1);
$qb = $conn->createQueryBuilder()
->select('u')
->from('users', 'u')
->where('u.id = :user_id1 OR u.id = :user_id2')
->setParameters(array(
':user_id1' => 1,
':user_id2' => 2
));
public setParameters ( array $params, array $types = [] ) | ||
$params | array | The query parameters to set. |
$types | array | The query parameters types to set. |
$qb = $conn->createQueryBuilder()
->insert('users')
->values(
array(
'name' => '?'
)
)
->setValue('password', '?');
$qb = $conn->createQueryBuilder()
->update('users', 'u')
->set('u.password', md5('password'))
->where('u.id = ?');
$qb = $conn->createQueryBuilder()
->insert('users')
->values(
array(
'name' => '?',
'password' => '?'
)
);
$qb = $conn->createQueryBuilder()
->select('u.name')
->from('users', 'u')
->where('u.id = ?');
You can optionally programatically build and/or expressions
$qb = $conn->createQueryBuilder();
$or = $qb->expr()->orx();
$or->add($qb->expr()->eq('u.id', 1));
$or->add($qb->expr()->eq('u.id', 2));
$qb->update('users', 'u')
->set('u.password', md5('password'))
->where($or);