Query builder¶
Query builder is a heart of every DBAL. It strives to support any SQL query.
Queries¶
SELECT query¶
Select query is started with issuing select() method.
SELECT queries are built from:
Example
<?php
$queryBuilder
->select('column')
->from('table')
->join('another', 'table.another_id = another.id', 'another')
->where($queryBuilder->condition('column', '=', '?'))
->orderBy('column', 'ASC')
->limit(1)
->setParameters('condition');
Note
See Conditions on how to construct WHERE conditions.
UPDATE query¶
Update query is started by issuing update() method.
UPDATE queries are built from:
Example
<?php
$queryBuilder
->update('table')
->set([
'columnA' => 'valueA',
'columnB' => 'valueB'
])
->where($queryBuilder->condition('columnC', '=', '?'))
->orderBy('columnD', 'ASC')
->limit(1)
->setParameters('condition');
INSERT query¶
Insert query is started by issuing insert() method.
INSERT queries are built from:
Example
DELETE query¶
Delete query is started by issuing delete() method.
DELETE queries are built from:
- FROM table to delete from
- WHERE conditions
- ORDER BY expressions
- LIMIT expressions
Example
<?php
$queryBuilder
->delete('table')
->where($queryBuilder->condition('column', '=', '?'))
->orderBy('column', 'ASC')
->limit(1)
->setParameters('condition');
Clauses¶
FROM clause¶
FROM clause defines a table or subquery (another QueryBuilder for SELECT statements).
INTO clause¶
INTO clause defines a table for INSERT queries. It's an alias for FROM, but it supports only table names and alias.
JOIN clause¶
JOIN clause is used to combine rows from tables based on related columns.
<?php
// inner join
public function join(string $table, string $on, string $alias = null, JoinType $type = JoinType::INNER): static;
// left join
public function leftJoin(string $table, string $on, string $alias = null): static;
// right join
public function rightJoin(string $table, string $on, string $alias = null): static;
// full join
public function fullJoin(string $table, string $on, string $alias = null): static;
VALUES/SET clause¶
Values are an associative array of column, value for INSERT or UPDATE queries.
<?php
public function values(array $values): static;
public function value(string $column, mixed $value): static; // add single value
public function set(array $values): static; // alias for values
WHERE clause¶
WHERE clause is used to filter records. Where clauses are built using conditions.