PHP Class yii\db\QueryBuilder

SQL statements are created from Query objects using the QueryBuilder::build-method. QueryBuilder is also used by Command to build SQL statements such as INSERT, UPDATE, DELETE, CREATE TABLE. For more details and usage information on QueryBuilder, see the guide article on query builders.
Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends yii\base\Object
Exibir arquivo Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$db the database connection.
$separator the separator between different fragments of a SQL statement. Defaults to an empty space. This is mainly used by QueryBuilder::build when generating a SQL statement.
$typeMap the abstract column types mapped to physical column types. This is mainly used to support creating/modifying tables using DB-independent data type specifications. Child classes should override this property to declare supported type mappings.

Protected Properties

Property Type Description
$conditionBuilders map of query condition to builder methods. These methods are used by [[buildCondition]] to build SQL conditions from array syntax.

Public Methods

Method Description
__construct ( Connection $connection, array $config = [] ) Constructor.
addColumn ( string $table, string $column, string $type ) : string Builds a SQL statement for adding a new DB column.
addCommentOnColumn ( string $table, string $column, string $comment ) : string Builds a SQL command for adding comment to column
addCommentOnTable ( string $table, string $comment ) : string Builds a SQL command for adding comment to table
addForeignKey ( string $name, string $table, string | array $columns, string $refTable, string | array $refColumns, string $delete = null, string $update = null ) : string Builds a SQL statement for adding a foreign key constraint to an existing table.
addPrimaryKey ( string $name, string $table, string | array $columns ) : string Builds a SQL statement for adding a primary key constraint to an existing table.
alterColumn ( string $table, string $column, string $type ) : string Builds a SQL statement for changing the definition of a column.
batchInsert ( string $table, array $columns, array $rows ) : string Generates a batch INSERT SQL statement.
build ( Query $query, array $params = [] ) : array Generates a SELECT SQL statement from a Query object.
buildAndCondition ( string $operator, array $operands, array &$params ) : string Connects two or more SQL expressions with the AND or OR operator.
buildBetweenCondition ( string $operator, array $operands, array &$params ) : string Creates an SQL expressions with the BETWEEN operator.
buildColumns ( string | array $columns ) : string Processes columns and properly quotes them if necessary.
buildCondition ( string | array | yii\db\Expression $condition, array &$params ) : string Parses the condition specification and generates the corresponding SQL expression.
buildExistsCondition ( string $operator, array $operands, array &$params ) : string Creates an SQL expressions with the EXISTS operator.
buildFrom ( array $tables, array &$params ) : string
buildGroupBy ( array $columns ) : string
buildHashCondition ( array $condition, array &$params ) : string Creates a condition based on column-value pairs.
buildHaving ( string | array $condition, array &$params ) : string
buildInCondition ( string $operator, array $operands, array &$params ) : string Creates an SQL expressions with the IN operator.
buildJoin ( array $joins, array &$params ) : string
buildLikeCondition ( string $operator, array $operands, array &$params ) : string Creates an SQL expressions with the LIKE operator.
buildLimit ( integer $limit, integer $offset ) : string
buildNotCondition ( string $operator, array $operands, array &$params ) : string Inverts an SQL expressions with NOT operator.
buildOrderBy ( array $columns ) : string
buildOrderByAndLimit ( string $sql, array $orderBy, integer $limit, integer $offset ) : string Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL.
buildSelect ( array $columns, array &$params, boolean $distinct = false, string $selectOption = null ) : string
buildSimpleCondition ( string $operator, array $operands, array &$params ) : string Creates an SQL expressions like "column" operator value.
buildUnion ( array $unions, array &$params ) : string
buildWhere ( string | array $condition, array &$params ) : string
checkIntegrity ( boolean $check = true, string $schema = '', string $table = '' ) : string Builds a SQL statement for enabling or disabling integrity check.
createIndex ( string $name, string $table, string | array $columns, boolean $unique = false ) : string Builds a SQL statement for creating a new index.
createTable ( string $table, array $columns, string $options = null ) : string Builds a SQL statement for creating a new DB table.
delete ( string $table, array | string $condition, array &$params ) : string Creates a DELETE SQL statement.
dropColumn ( string $table, string $column ) : string Builds a SQL statement for dropping a DB column.
dropCommentFromColumn ( string $table, string $column ) : string Builds a SQL command for adding comment to column
dropCommentFromTable ( string $table ) : string Builds a SQL command for adding comment to table
dropForeignKey ( string $name, string $table ) : string Builds a SQL statement for dropping a foreign key constraint.
dropIndex ( string $name, string $table ) : string Builds a SQL statement for dropping an index.
dropPrimaryKey ( string $name, string $table ) : string Builds a SQL statement for removing a primary key constraint to an existing table.
dropTable ( string $table ) : string Builds a SQL statement for dropping a DB table.
getColumnType ( string | ColumnSchemaBuilder $type ) : string Converts an abstract column type into a physical column type.
insert ( string $table, array $columns, array &$params ) : string Creates an INSERT SQL statement.
renameColumn ( string $table, string $oldName, string $newName ) : string Builds a SQL statement for renaming a column.
renameTable ( string $oldName, string $newName ) : string Builds a SQL statement for renaming a DB table.
resetSequence ( string $table, array | string $value = null ) : string Creates a SQL statement for resetting the sequence value of a table's primary key.
selectExists ( string $rawSql ) : string Creates a SELECT EXISTS() SQL statement.
truncateTable ( string $table ) : string Builds a SQL statement for truncating a DB table.
update ( string $table, array $columns, array | string $condition, array &$params ) : string Creates an UPDATE SQL statement.

Protected Methods

Method Description
buildCompositeInCondition ( string $operator, array | Traversable $columns, array $values, array &$params ) : string Builds SQL for IN condition
buildSubqueryInCondition ( string $operator, array $columns, Query $values, array &$params ) : string Builds SQL for IN condition
hasLimit ( mixed $limit ) : boolean Checks to see if the given limit is effective.
hasOffset ( mixed $offset ) : boolean Checks to see if the given offset is effective.

Private Methods

Method Description
quoteTableNames ( array $tables, array &$params ) : array Quotes table names passed

Method Details

__construct() public method

Constructor.
public __construct ( Connection $connection, array $config = [] )
$connection Connection the database connection.
$config array name-value pairs that will be used to initialize the object properties

addColumn() public method

Builds a SQL statement for adding a new DB column.
public addColumn ( string $table, string $column, string $type ) : string
$table string the table that the new column will be added to. The table name will be properly quoted by the method.
$column string the name of the new column. The name will be properly quoted by the method.
$type string the column type. The [[getColumnType()]] method will be invoked to convert abstract column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'.
return string the SQL statement for adding a new column.

addCommentOnColumn() public method

Builds a SQL command for adding comment to column
Since: 2.0.8
public addCommentOnColumn ( string $table, string $column, string $comment ) : string
$table string the table whose column is to be commented. The table name will be properly quoted by the method.
$column string the name of the column to be commented. The column name will be properly quoted by the method.
$comment string the text of the comment to be added. The comment will be properly quoted by the method.
return string the SQL statement for adding comment on column

addCommentOnTable() public method

Builds a SQL command for adding comment to table
Since: 2.0.8
public addCommentOnTable ( string $table, string $comment ) : string
$table string the table whose column is to be commented. The table name will be properly quoted by the method.
$comment string the text of the comment to be added. The comment will be properly quoted by the method.
return string the SQL statement for adding comment on table

addForeignKey() public method

The method will properly quote the table and column names.
public addForeignKey ( string $name, string $table, string | array $columns, string $refTable, string | array $refColumns, string $delete = null, string $update = null ) : string
$name string the name of the foreign key constraint.
$table string the table that the foreign key constraint will be added to.
$columns string | array the name of the column to that the constraint will be added on. If there are multiple columns, separate them with commas or use an array to represent them.
$refTable string the table that the foreign key references to.
$refColumns string | array the name of the column that the foreign key references to. If there are multiple columns, separate them with commas or use an array to represent them.
$delete string the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
$update string the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
return string the SQL statement for adding a foreign key constraint to an existing table.

addPrimaryKey() public method

Builds a SQL statement for adding a primary key constraint to an existing table.
public addPrimaryKey ( string $name, string $table, string | array $columns ) : string
$name string the name of the primary key constraint.
$table string the table that the primary key constraint will be added to.
$columns string | array comma separated string or array of columns that the primary key will consist of.
return string the SQL statement for adding a primary key constraint to an existing table.

alterColumn() public method

Builds a SQL statement for changing the definition of a column.
public alterColumn ( string $table, string $column, string $type ) : string
$table string the table whose column is to be changed. The table name will be properly quoted by the method.
$column string the name of the column to be changed. The name will be properly quoted by the method.
$type string the new column type. The [[getColumnType()]] method will be invoked to convert abstract column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'.
return string the SQL statement for changing the definition of a column.

batchInsert() public method

For example, php $sql = $queryBuilder->batchInsert('user', ['name', 'age'], [ ['Tom', 30], ['Jane', 20], ['Linda', 25], ]); Note that the values in each row must match the corresponding column names. The method will properly escape the column names, and quote the values to be inserted.
public batchInsert ( string $table, array $columns, array $rows ) : string
$table string the table that new rows will be inserted into.
$columns array the column names
$rows array the rows to be batch inserted into the table
return string the batch INSERT SQL statement

build() public method

Generates a SELECT SQL statement from a Query object.
public build ( Query $query, array $params = [] ) : array
$query Query the [[Query]] object from which the SQL statement will be generated.
$params array the parameters to be bound to the generated SQL statement. These parameters will be included in the result with the additional parameters generated during the query building process.
return array the generated SQL statement (the first array element) and the corresponding parameters to be bound to the SQL statement (the second array element). The parameters returned include those provided in `$params`.

buildAndCondition() public method

Connects two or more SQL expressions with the AND or OR operator.
public buildAndCondition ( string $operator, array $operands, array &$params ) : string
$operator string the operator to use for connecting the given operands
$operands array the SQL expressions to connect.
$params array the binding parameters to be populated
return string the generated SQL expression

buildBetweenCondition() public method

Creates an SQL expressions with the BETWEEN operator.
public buildBetweenCondition ( string $operator, array $operands, array &$params ) : string
$operator string the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`)
$operands array the first operand is the column name. The second and third operands describe the interval that column value should be in.
$params array the binding parameters to be populated
return string the generated SQL expression

buildColumns() public method

It will join all columns into a string with comma as separators.
public buildColumns ( string | array $columns ) : string
$columns string | array the columns to be processed
return string the processing result

buildCompositeInCondition() protected method

Builds SQL for IN condition
protected buildCompositeInCondition ( string $operator, array | Traversable $columns, array $values, array &$params ) : string
$operator string
$columns array | Traversable
$values array
$params array
return string SQL

buildCondition() public method

Parses the condition specification and generates the corresponding SQL expression.
public buildCondition ( string | array | yii\db\Expression $condition, array &$params ) : string
$condition string | array | yii\db\Expression the condition specification. Please refer to [[Query::where()]] on how to specify a condition.
$params array the binding parameters to be populated
return string the generated SQL expression

buildExistsCondition() public method

Creates an SQL expressions with the EXISTS operator.
public buildExistsCondition ( string $operator, array $operands, array &$params ) : string
$operator string the operator to use (e.g. `EXISTS` or `NOT EXISTS`)
$operands array contains only one element which is a [[Query]] object representing the sub-query.
$params array the binding parameters to be populated
return string the generated SQL expression

buildFrom() public method

public buildFrom ( array $tables, array &$params ) : string
$tables array
$params array the binding parameters to be populated
return string the FROM clause built from [[Query::$from]].

buildGroupBy() public method

public buildGroupBy ( array $columns ) : string
$columns array
return string the GROUP BY clause

buildHashCondition() public method

Creates a condition based on column-value pairs.
public buildHashCondition ( array $condition, array &$params ) : string
$condition array the condition specification.
$params array the binding parameters to be populated
return string the generated SQL expression

buildHaving() public method

public buildHaving ( string | array $condition, array &$params ) : string
$condition string | array
$params array the binding parameters to be populated
return string the HAVING clause built from [[Query::$having]].

buildInCondition() public method

Creates an SQL expressions with the IN operator.
public buildInCondition ( string $operator, array $operands, array &$params ) : string
$operator string the operator to use (e.g. `IN` or `NOT IN`)
$operands array the first operand is the column name. If it is an array a composite IN condition will be generated. The second operand is an array of values that column value should be among. If it is an empty array the generated expression will be a `false` value if operator is `IN` and empty if operator is `NOT IN`.
$params array the binding parameters to be populated
return string the generated SQL expression

buildJoin() public method

public buildJoin ( array $joins, array &$params ) : string
$joins array
$params array the binding parameters to be populated
return string the JOIN clause built from [[Query::$join]].

buildLikeCondition() public method

Creates an SQL expressions with the LIKE operator.
public buildLikeCondition ( string $operator, array $operands, array &$params ) : string
$operator string the operator to use (e.g. `LIKE`, `NOT LIKE`, `OR LIKE` or `OR NOT LIKE`)
$operands array an array of two or three operands - The first operand is the column name. - The second operand is a single value or an array of values that column value should be compared with. If it is an empty array the generated expression will be a `false` value if operator is `LIKE` or `OR LIKE`, and empty if operator is `NOT LIKE` or `OR NOT LIKE`. - An optional third operand can also be provided to specify how to escape special characters in the value(s). The operand should be an array of mappings from the special characters to their escaped counterparts. If this operand is not provided, a default escape mapping will be used. You may use `false` or an empty array to indicate the values are already escaped and no escape should be applied. Note that when using an escape mapping (or the third operand is not provided), the values will be automatically enclosed within a pair of percentage characters.
$params array the binding parameters to be populated
return string the generated SQL expression

buildLimit() public method

public buildLimit ( integer $limit, integer $offset ) : string
$limit integer
$offset integer
return string the LIMIT and OFFSET clauses

buildNotCondition() public method

Inverts an SQL expressions with NOT operator.
public buildNotCondition ( string $operator, array $operands, array &$params ) : string
$operator string the operator to use for connecting the given operands
$operands array the SQL expressions to connect.
$params array the binding parameters to be populated
return string the generated SQL expression

buildOrderBy() public method

public buildOrderBy ( array $columns ) : string
$columns array
return string the ORDER BY clause built from [[Query::$orderBy]].

buildOrderByAndLimit() public method

Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL.
public buildOrderByAndLimit ( string $sql, array $orderBy, integer $limit, integer $offset ) : string
$sql string the existing SQL (without ORDER BY/LIMIT/OFFSET)
$orderBy array the order by columns. See [[Query::orderBy]] for more details on how to specify this parameter.
$limit integer the limit number. See [[Query::limit]] for more details.
$offset integer the offset number. See [[Query::offset]] for more details.
return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any)

buildSelect() public method

public buildSelect ( array $columns, array &$params, boolean $distinct = false, string $selectOption = null ) : string
$columns array
$params array the binding parameters to be populated
$distinct boolean
$selectOption string
return string the SELECT clause built from [[Query::$select]].

buildSimpleCondition() public method

Creates an SQL expressions like "column" operator value.
public buildSimpleCondition ( string $operator, array $operands, array &$params ) : string
$operator string the operator to use. Anything could be used e.g. `>`, `<=`, etc.
$operands array contains two column names.
$params array the binding parameters to be populated
return string the generated SQL expression

buildSubqueryInCondition() protected method

Builds SQL for IN condition
protected buildSubqueryInCondition ( string $operator, array $columns, Query $values, array &$params ) : string
$operator string
$columns array
$values Query
$params array
return string SQL

buildUnion() public method

public buildUnion ( array $unions, array &$params ) : string
$unions array
$params array the binding parameters to be populated
return string the UNION clause built from [[Query::$union]].

buildWhere() public method

public buildWhere ( string | array $condition, array &$params ) : string
$condition string | array
$params array the binding parameters to be populated
return string the WHERE clause built from [[Query::$where]].

checkIntegrity() public method

Builds a SQL statement for enabling or disabling integrity check.
public checkIntegrity ( boolean $check = true, string $schema = '', string $table = '' ) : string
$check boolean whether to turn on or off the integrity check.
$schema string the schema of the tables. Defaults to empty string, meaning the current or default schema.
$table string the table name. Defaults to empty string, meaning that no table will be changed.
return string the SQL statement for checking integrity

createIndex() public method

Builds a SQL statement for creating a new index.
public createIndex ( string $name, string $table, string | array $columns, boolean $unique = false ) : string
$name string the name of the index. The name will be properly quoted by the method.
$table string the table that the new index will be created for. The table name will be properly quoted by the method.
$columns string | array the column(s) that should be included in the index. If there are multiple columns, separate them with commas or use an array to represent them. Each column name will be properly quoted by the method, unless a parenthesis is found in the name.
$unique boolean whether to add UNIQUE constraint on the created index.
return string the SQL statement for creating a new index.

createTable() public method

The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), where name stands for a column name which will be properly quoted by the method, and definition stands for the column type which can contain an abstract DB type. The QueryBuilder::getColumnType method will be invoked to convert any abstract type into a physical one. If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly inserted into the generated SQL. For example, php $sql = $queryBuilder->createTable('user', [ 'id' => 'pk', 'name' => 'string', 'age' => 'integer', ]);
public createTable ( string $table, array $columns, string $options = null ) : string
$table string the name of the table to be created. The name will be properly quoted by the method.
$columns array the columns (name => definition) in the new table.
$options string additional SQL fragment that will be appended to the generated SQL.
return string the SQL statement for creating a new DB table.

delete() public method

For example, php $sql = $queryBuilder->delete('user', 'status = 0'); The method will properly escape the table and column names.
public delete ( string $table, array | string $condition, array &$params ) : string
$table string the table where the data will be deleted from.
$condition array | string the condition that will be put in the WHERE part. Please refer to [[Query::where()]] on how to specify condition.
$params array the binding parameters that will be modified by this method so that they can be bound to the DB command later.
return string the DELETE SQL

dropColumn() public method

Builds a SQL statement for dropping a DB column.
public dropColumn ( string $table, string $column ) : string
$table string the table whose column is to be dropped. The name will be properly quoted by the method.
$column string the name of the column to be dropped. The name will be properly quoted by the method.
return string the SQL statement for dropping a DB column.

dropCommentFromColumn() public method

Builds a SQL command for adding comment to column
Since: 2.0.8
public dropCommentFromColumn ( string $table, string $column ) : string
$table string the table whose column is to be commented. The table name will be properly quoted by the method.
$column string the name of the column to be commented. The column name will be properly quoted by the method.
return string the SQL statement for adding comment on column

dropCommentFromTable() public method

Builds a SQL command for adding comment to table
Since: 2.0.8
public dropCommentFromTable ( string $table ) : string
$table string the table whose column is to be commented. The table name will be properly quoted by the method.
return string the SQL statement for adding comment on column

dropForeignKey() public method

Builds a SQL statement for dropping a foreign key constraint.
public dropForeignKey ( string $name, string $table ) : string
$name string the name of the foreign key constraint to be dropped. The name will be properly quoted by the method.
$table string the table whose foreign is to be dropped. The name will be properly quoted by the method.
return string the SQL statement for dropping a foreign key constraint.

dropIndex() public method

Builds a SQL statement for dropping an index.
public dropIndex ( string $name, string $table ) : string
$name string the name of the index to be dropped. The name will be properly quoted by the method.
$table string the table whose index is to be dropped. The name will be properly quoted by the method.
return string the SQL statement for dropping an index.

dropPrimaryKey() public method

Builds a SQL statement for removing a primary key constraint to an existing table.
public dropPrimaryKey ( string $name, string $table ) : string
$name string the name of the primary key constraint to be removed.
$table string the table that the primary key constraint will be removed from.
return string the SQL statement for removing a primary key constraint from an existing table.

dropTable() public method

Builds a SQL statement for dropping a DB table.
public dropTable ( string $table ) : string
$table string the table to be dropped. The name will be properly quoted by the method.
return string the SQL statement for dropping a DB table.

getColumnType() public method

The conversion is done using the type map specified in [[typeMap]]. The following abstract column types are supported (using MySQL as an example to explain the corresponding physical types): - pk: an auto-incremental primary key type, will be converted into "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY" - bigpk: an auto-incremental primary key type, will be converted into "bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY" - unsignedpk: an unsigned auto-incremental primary key type, will be converted into "int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY" - char: char type, will be converted into "char(1)" - string: string type, will be converted into "varchar(255)" - text: a long string type, will be converted into "text" - smallint: a small integer type, will be converted into "smallint(6)" - integer: integer type, will be converted into "int(11)" - bigint: a big integer type, will be converted into "bigint(20)" - boolean: boolean type, will be converted into "tinyint(1)" - float: float number type, will be converted into "float" - decimal: decimal number type, will be converted into "decimal" - datetime: datetime type, will be converted into "datetime" - timestamp: timestamp type, will be converted into "timestamp" - time: time type, will be converted into "time" - date: date type, will be converted into "date" - money: money type, will be converted into "decimal(19,4)" - binary: binary data type, will be converted into "blob" If the abstract type contains two or more parts separated by spaces (e.g. "string NOT NULL"), then only the first part will be converted, and the rest of the parts will be appended to the converted result. For example, 'string NOT NULL' is converted to 'varchar(255) NOT NULL'. For some of the abstract types you can also specify a length or precision constraint by appending it in round brackets directly to the type. For example string(32) will be converted into "varchar(32)" on a MySQL database. If the underlying DBMS does not support these kind of constraints for a type it will be ignored. If a type cannot be found in [[typeMap]], it will be returned without any change.
public getColumnType ( string | ColumnSchemaBuilder $type ) : string
$type string | ColumnSchemaBuilder abstract column type
return string physical column type.

hasLimit() protected method

Checks to see if the given limit is effective.
protected hasLimit ( mixed $limit ) : boolean
$limit mixed the given limit
return boolean whether the limit is effective

hasOffset() protected method

Checks to see if the given offset is effective.
protected hasOffset ( mixed $offset ) : boolean
$offset mixed the given offset
return boolean whether the offset is effective

insert() public method

For example, php $sql = $queryBuilder->insert('user', [ 'name' => 'Sam', 'age' => 30, ], $params); The method will properly escape the table and column names.
public insert ( string $table, array $columns, array &$params ) : string
$table string the table that new rows will be inserted into.
$columns array the column data (name => value) to be inserted into the table.
$params array the binding parameters that will be generated by this method. They should be bound to the DB command later.
return string the INSERT SQL

renameColumn() public method

Builds a SQL statement for renaming a column.
public renameColumn ( string $table, string $oldName, string $newName ) : string
$table string the table whose column is to be renamed. The name will be properly quoted by the method.
$oldName string the old name of the column. The name will be properly quoted by the method.
$newName string the new name of the column. The name will be properly quoted by the method.
return string the SQL statement for renaming a DB column.

renameTable() public method

Builds a SQL statement for renaming a DB table.
public renameTable ( string $oldName, string $newName ) : string
$oldName string the table to be renamed. The name will be properly quoted by the method.
$newName string the new table name. The name will be properly quoted by the method.
return string the SQL statement for renaming a DB table.

resetSequence() public method

The sequence will be reset such that the primary key of the next new row inserted will have the specified value or 1.
public resetSequence ( string $table, array | string $value = null ) : string
$table string the name of the table whose primary key sequence will be reset
$value array | string the value for the primary key of the next new row inserted. If this is not set, the next new row's primary key will have a value 1.
return string the SQL statement for resetting sequence

selectExists() public method

Creates a SELECT EXISTS() SQL statement.
Since: 2.0.8
public selectExists ( string $rawSql ) : string
$rawSql string the subquery in a raw form to select from.
return string the SELECT EXISTS() SQL statement.

truncateTable() public method

Builds a SQL statement for truncating a DB table.
public truncateTable ( string $table ) : string
$table string the table to be truncated. The name will be properly quoted by the method.
return string the SQL statement for truncating a DB table.

update() public method

For example, php $params = []; $sql = $queryBuilder->update('user', ['status' => 1], 'age > 30', $params); The method will properly escape the table and column names.
public update ( string $table, array $columns, array | string $condition, array &$params ) : string
$table string the table to be updated.
$columns array the column data (name => value) to be updated.
$condition array | string the condition that will be put in the WHERE part. Please refer to [[Query::where()]] on how to specify condition.
$params array the binding parameters that will be modified by this method so that they can be bound to the DB command later.
return string the UPDATE SQL

Property Details

$conditionBuilders protected_oe property

map of query condition to builder methods. These methods are used by [[buildCondition]] to build SQL conditions from array syntax.
protected $conditionBuilders

$db public_oe property

the database connection.
public $db

$separator public_oe property

the separator between different fragments of a SQL statement. Defaults to an empty space. This is mainly used by QueryBuilder::build when generating a SQL statement.
public $separator

$typeMap public_oe property

the abstract column types mapped to physical column types. This is mainly used to support creating/modifying tables using DB-independent data type specifications. Child classes should override this property to declare supported type mappings.
public $typeMap