‪TYPO3CMS  ‪main
TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder Class Reference
Inheritance diagram for TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder:

Public Member Functions

 __construct (protected readonly Connection $connection)
 
 __clone ()
 
$this select (string ... $expressions)
 
$this distinct (bool $distinct=true)
 
$this addSelect (string $expression, string ... $expressions)
 
$this delete (string $table)
 
$this update (string $table)
 
$this insert (string $table)
 
$this from (string $table, ?string $alias=null)
 
$this innerJoin (string $fromAlias, string $join, string $alias, ?string $condition=null)
 
$this leftJoin (string $fromAlias, string $join, string $alias, ?string $condition=null)
 
$this rightJoin (string $fromAlias, string $join, string $alias, ?string $condition=null)
 
$this where (string|CompositeExpression $predicate, string|CompositeExpression ... $predicates)
 
$this andWhere (string|CompositeExpression $predicate, string|CompositeExpression ... $predicates)
 
$this orWhere (string|CompositeExpression $predicate, string|CompositeExpression ... $predicates)
 
$this groupBy (string $expression, string ... $expressions)
 
$this addGroupBy (string $expression, string ... $expressions)
 
$this having (string|CompositeExpression $predicate, string|CompositeExpression ... $predicates)
 
$this andHaving (string|CompositeExpression $predicate, string|CompositeExpression ... $predicates)
 
$this orHaving (string|CompositeExpression $predicate, string|CompositeExpression ... $predicates)
 
$this orderBy (string $sort, ?string $order=null)
 
$this addOrderBy (string $sort, ?string $order=null)
 
$this resetWhere ()
 
$this resetGroupBy ()
 
$this resetHaving ()
 
$this resetOrderBy ()
 

Static Protected Member Functions

static bool isEmptyPart (CompositeExpression|string|null $value)
 

Protected Attributes

QueryType $type = QueryType::SELECT
 
array $select = []
 
bool $distinct = false
 
array $from = []
 
array $join = []
 
string CompositeExpression null $where = null
 
array $groupBy = []
 
string CompositeExpression null $having = null
 
array $orderBy = []
 

Private Member Functions

 createPredicate (string|CompositeExpression $predicate, string|CompositeExpression ... $predicates,)
 
 appendToPredicate (string|CompositeExpression|null $currentPredicate, string $type, string|CompositeExpression ... $predicates,)
 

Detailed Description

QueryBuilder class is responsible to dynamically create SQL queries.

Important: Verify that every feature you use will work with your database vendor. SQL Query Builder does not attempt to validate the generated SQL at all.

The query builder does no validation whatsoever if certain features even work with the underlying database vendor. Limit queries and joins are NOT applied to UPDATE and DELETE statements even if some vendors such as MySQL support it.

not part of public core API. Uses as intermediate decorator wrapper to keep track of state, which is considered internal and therefore by Doctrine DBAL but TYPO3 requires internal access.

Definition at line 40 of file ConcreteQueryBuilder.php.

Constructor & Destructor Documentation

◆ __construct()

TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::__construct ( protected readonly Connection  $connection)

Initializes a new QueryBuilder.

Parameters
Connection$connection‪The DBAL Connection.

Definition at line 102 of file ConcreteQueryBuilder.php.

Member Function Documentation

◆ __clone()

◆ addGroupBy()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::addGroupBy ( string  $expression,
string ...  $expressions 
)

Adds one or more grouping expressions to the query.

$qb = $conn->createQueryBuilder() ->select('u.name') ->from('users', 'u') ->groupBy('u.lastLogin') ->addGroupBy('u.createdAt');

Parameters
string$expression‪The grouping expression
string‪...$expressions Additional grouping expressions
Returns
‪$this This QueryBuilder instance.

Definition at line 492 of file ConcreteQueryBuilder.php.

References TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\$groupBy, and TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\groupBy().

◆ addOrderBy()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::addOrderBy ( string  $sort,
?string  $order = null 
)

Adds an ordering to the query results.

Parameters
string$sort‪The ordering expression.
string$order‪The ordering direction.
Returns
‪$this This QueryBuilder instance.

Definition at line 619 of file ConcreteQueryBuilder.php.

References TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\$orderBy, and TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\orderBy().

◆ addSelect()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::addSelect ( string  $expression,
string ...  $expressions 
)

Adds an item that is to be returned in the query result.

$qb = $conn->createQueryBuilder() ->select('u.id') ->addSelect('p.id') ->from('users', 'u') ->leftJoin('u', 'phonenumbers', 'u.id = p.user_id');

Parameters
string$expression‪The selection expression.
string‪...$expressions Additional selection expressions.
Returns
‪$this This QueryBuilder instance.

Definition at line 190 of file ConcreteQueryBuilder.php.

References TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\select().

◆ andHaving()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::andHaving ( string|CompositeExpression  $predicate,
string|CompositeExpression ...  $predicates 
)

Adds a restriction over the groups of the query, forming a logical conjunction with any existing having restrictions.

Parameters
string | CompositeExpression$predicate‪The predicate to append.
string|CompositeExpression‪...$predicates Additional predicates to append.
Returns
‪$this This QueryBuilder instance.

Definition at line 524 of file ConcreteQueryBuilder.php.

References TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\$having, TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\appendToPredicate(), and TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\having().

◆ andWhere()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::andWhere ( string|CompositeExpression  $predicate,
string|CompositeExpression ...  $predicates 
)

Adds one or more restrictions to the query results, forming a logical conjunction with any previously specified restrictions.

$qb = $conn->createQueryBuilder() ->select('u') ->from('users', 'u') ->where('u.username LIKE ?') ->andWhere('u.is_active = 1');

See also
where()
Parameters
string | CompositeExpression$predicate‪The predicate to append.
string|CompositeExpression‪...$predicates Additional predicates to append.
Returns
‪$this This QueryBuilder instance.

Definition at line 415 of file ConcreteQueryBuilder.php.

References TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\$where, TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\appendToPredicate(), and TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\where().

◆ appendToPredicate()

TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::appendToPredicate ( string|CompositeExpression|null  $currentPredicate,
string  $type,
string|CompositeExpression ...  $predicates 
)
private

◆ createPredicate()

TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::createPredicate ( string|CompositeExpression  $predicate,
string|CompositeExpression ...  $predicates 
)
private

Creates a CompositeExpression from one or more predicates combined by the AND logic.

Definition at line 560 of file ConcreteQueryBuilder.php.

Referenced by TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\having(), and TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\where().

◆ delete()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::delete ( string  $table)

Turns the query being built into a bulk delete query that ranges over a certain table.

$qb = $conn->createQueryBuilder() ->delete('users', 'u') ->where('u.id = :user_id') ->setParameter(':user_id', 1);

Parameters
string$table‪The table whose rows are subject to the deletion.
Returns
‪$this This QueryBuilder instance.

Definition at line 213 of file ConcreteQueryBuilder.php.

◆ distinct()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::distinct ( bool  $distinct = true)

Adds or removes DISTINCT to/from the query.

$qb = $conn->createQueryBuilder() ->select('u.id') ->distinct() ->from('users', 'u')

Returns
‪$this This QueryBuilder instance.

Definition at line 167 of file ConcreteQueryBuilder.php.

References TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\$distinct.

◆ from()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::from ( string  $table,
?string  $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.

$qb = $conn->createQueryBuilder() ->select('u.id') ->from('users', 'u')

Parameters
string$table‪The table.
string | null$alias‪The alias of the table.
Returns
‪$this This QueryBuilder instance.

Definition at line 283 of file ConcreteQueryBuilder.php.

Referenced by TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\__clone().

◆ groupBy()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::groupBy ( string  $expression,
string ...  $expressions 
)

Specifies one or more grouping expressions over the results of the query. Replaces any previously specified groupings, if any.

$qb = $conn->createQueryBuilder() ->select('u.name') ->from('users', 'u') ->groupBy('u.id');

Parameters
string$expression‪The grouping expression
string‪...$expressions Additional grouping expressions
Returns
‪$this This QueryBuilder instance.

Definition at line 469 of file ConcreteQueryBuilder.php.

References TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\$groupBy.

Referenced by TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\addGroupBy(), and TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\resetGroupBy().

◆ having()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::having ( string|CompositeExpression  $predicate,
string|CompositeExpression ...  $predicates 
)

Specifies a restriction over the groups of the query. Replaces any previous having restrictions, if any.

Parameters
string | CompositeExpression$predicate‪The HAVING clause predicate.
string|CompositeExpression‪...$predicates Additional HAVING clause predicates.
Returns
‪$this This QueryBuilder instance.

Definition at line 508 of file ConcreteQueryBuilder.php.

References TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\$having, and TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\createPredicate().

Referenced by TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\__clone(), TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\andHaving(), TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\orHaving(), and TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\resetHaving().

◆ innerJoin()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::innerJoin ( string  $fromAlias,
string  $join,
string  $alias,
?string  $condition = null 
)

Creates and adds a join to the query.

$qb = $conn->createQueryBuilder() ->select('u.name') ->from('users', 'u') ->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');

Parameters
string$fromAlias‪The alias that points to a from clause.
string$join‪The table name to join.
string$alias‪The alias of the join table.
string$condition‪The condition for the join.
Returns
‪$this This QueryBuilder instance.

Definition at line 307 of file ConcreteQueryBuilder.php.

References TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\$join.

◆ insert()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::insert ( string  $table)

Turns the query being built into an insert query that inserts into a certain table

$qb = $conn->createQueryBuilder() ->insert('users') ->values( array( 'name' => '?', 'password' => '?' ) );

Parameters
string$table‪The table into which the rows should be inserted.
Returns
‪$this This QueryBuilder instance.

Definition at line 261 of file ConcreteQueryBuilder.php.

◆ isEmptyPart()

static bool TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::isEmptyPart ( CompositeExpression|string|null  $value)
staticprotected

Determine if a query part used for where or having is empty. Used as array_filter in ConcreteQueryBuilder methods. This is needed to avoid invalid sql syntax by empty parts, which can happen to relaxed custom CompositeExpression handling.

For example used to avoid : (uid = 1) and () and (pid = 2).

See also
ConcreteQueryBuilder::createPredicate()
ConcreteQueryBuilder::appendToPredicate()
‪\TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression::isEmptyPart()
Parameters
CompositeExpression | string | null$value
Returns
‪bool

Definition at line 696 of file ConcreteQueryBuilder.php.

◆ leftJoin()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::leftJoin ( string  $fromAlias,
string  $join,
string  $alias,
?string  $condition = null 
)

Creates and adds a left join to the query.

$qb = $conn->createQueryBuilder() ->select('u.name') ->from('users', 'u') ->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');

Parameters
string$fromAlias‪The alias that points to a from clause.
string$join‪The table name to join.
string$alias‪The alias of the join table.
string$condition‪The condition for the join.
Returns
‪$this This QueryBuilder instance.

Definition at line 331 of file ConcreteQueryBuilder.php.

References TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\$join.

◆ orderBy()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::orderBy ( string  $sort,
?string  $order = null 
)

Specifies an ordering for the query results. Replaces any previously specified orderings, if any.

Parameters
string$sort‪The ordering expression.
string$order‪The ordering direction.
Returns
‪$this This QueryBuilder instance.

Definition at line 600 of file ConcreteQueryBuilder.php.

References TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\$orderBy.

Referenced by TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\addOrderBy(), and TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\resetOrderBy().

◆ orHaving()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::orHaving ( string|CompositeExpression  $predicate,
string|CompositeExpression ...  $predicates 
)

Adds a restriction over the groups of the query, forming a logical disjunction with any existing having restrictions.

Parameters
string | CompositeExpression$predicate‪The predicate to append.
string|CompositeExpression‪...$predicates Additional predicates to append.
Returns
‪$this This QueryBuilder instance.

Definition at line 545 of file ConcreteQueryBuilder.php.

References TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\$having, TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\appendToPredicate(), and TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\having().

◆ orWhere()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::orWhere ( string|CompositeExpression  $predicate,
string|CompositeExpression ...  $predicates 
)

Adds one or more restrictions to the query results, forming a logical disjunction with any previously specified restrictions.

$qb = $em->createQueryBuilder() ->select('u.name') ->from('users', 'u') ->where('u.id = 1') ->orWhere('u.id = 2');

See also
where()
Parameters
string | CompositeExpression$predicate‪The predicate to append.
string|CompositeExpression‪...$predicates Additional predicates to append.
Returns
‪$this This QueryBuilder instance.

Definition at line 446 of file ConcreteQueryBuilder.php.

References TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\$where, TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\appendToPredicate(), and TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\where().

◆ resetGroupBy()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::resetGroupBy ( )

Resets the grouping for the query.

Returns
‪$this This QueryBuilder instance.

Definition at line 647 of file ConcreteQueryBuilder.php.

References TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\groupBy().

◆ resetHaving()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::resetHaving ( )

Resets the HAVING conditions for the query.

Returns
‪$this This QueryBuilder instance.

Definition at line 659 of file ConcreteQueryBuilder.php.

References TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\having().

◆ resetOrderBy()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::resetOrderBy ( )

Resets the ordering for the query.

Returns
‪$this This QueryBuilder instance.

Definition at line 671 of file ConcreteQueryBuilder.php.

References TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\orderBy().

◆ resetWhere()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::resetWhere ( )

Resets the WHERE conditions for the query.

Returns
‪$this This QueryBuilder instance.

Definition at line 635 of file ConcreteQueryBuilder.php.

References TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\where().

◆ rightJoin()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::rightJoin ( string  $fromAlias,
string  $join,
string  $alias,
?string  $condition = null 
)

Creates and adds a right join to the query.

$qb = $conn->createQueryBuilder() ->select('u.name') ->from('users', 'u') ->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');

Parameters
string$fromAlias‪The alias that points to a from clause.
string$join‪The table name to join.
string$alias‪The alias of the join table.
string$condition‪The condition for the join.
Returns
‪$this This QueryBuilder instance.

Definition at line 355 of file ConcreteQueryBuilder.php.

References TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\$join.

◆ select()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::select ( string ...  $expressions)

Specifies an item that is to be returned in the query result. Replaces any previously specified selections, if any.

$qb = $conn->createQueryBuilder() ->select('u.id', 'p.id') ->from('users', 'u') ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id');

Parameters
string‪...$expressions The selection expressions.
Returns
‪$this This QueryBuilder instance.

Definition at line 144 of file ConcreteQueryBuilder.php.

Referenced by TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\addSelect().

◆ update()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::update ( string  $table)

Turns the query being built into a bulk update query that ranges over a certain table

$qb = $conn->createQueryBuilder() ->update('counters', 'c') ->set('c.value', 'c.value + 1') ->where('c.id = ?');

Parameters
string$table‪The table whose rows are subject to the update.
Returns
‪$this This QueryBuilder instance.

Definition at line 235 of file ConcreteQueryBuilder.php.

◆ where()

$this TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::where ( string|CompositeExpression  $predicate,
string|CompositeExpression ...  $predicates 
)

Specifies one or more restrictions to the query result. Replaces any previously specified restrictions, if any.

$qb = $conn->createQueryBuilder() ->select('c.value') ->from('counters', 'c') ->where('c.id = ?');

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

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

$qb->update('counters', 'c') ->set('c.value', 'c.value + 1') ->where($or);

Parameters
string | CompositeExpression$predicate‪The WHERE clause predicate.
string|CompositeExpression‪...$predicates Additional WHERE clause predicates.
Returns
‪$this This QueryBuilder instance.

Definition at line 389 of file ConcreteQueryBuilder.php.

References TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\$where, and TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\createPredicate().

Referenced by TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\__clone(), TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\andWhere(), TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\orWhere(), and TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\resetWhere().

Member Data Documentation

◆ $distinct

bool TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::$distinct = false
protected

Whether this is a SELECT DISTINCT query.

Definition at line 57 of file ConcreteQueryBuilder.php.

Referenced by TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\distinct().

◆ $from

array TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::$from = []
protected

◆ $groupBy

array TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::$groupBy = []
protected

◆ $having

string CompositeExpression null TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::$having = null
protected

◆ $join

◆ $orderBy

array TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::$orderBy = []
protected

◆ $select

array TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::$select = []
protected

Definition at line 52 of file ConcreteQueryBuilder.php.

◆ $type

QueryType TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::$type = QueryType::SELECT
protected

The type of query this is. Can be select, update or delete.

Definition at line 45 of file ConcreteQueryBuilder.php.

Referenced by TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder\appendToPredicate().

◆ $where

string CompositeExpression null TYPO3\CMS\Core\Database\Query\ConcreteQueryBuilder::$where = null
protected