Inheritance | yii\db\QueryBuilder » yii\base\Object |
---|---|
Implements | yii\base\Configurable |
Subclasses | yii\db\cubrid\QueryBuilder, yii\db\mssql\QueryBuilder, yii\db\mysql\QueryBuilder, yii\db\oci\QueryBuilder, yii\db\pgsql\QueryBuilder, yii\db\sqlite\QueryBuilder |
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/db/QueryBuilder.php |
QueryBuilder builds a SELECT SQL statement based on the specification given as a yii\db\Query object.
SQL statements are created from yii\db\Query objects using the build()-method.
QueryBuilder is also used by yii\db\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.
Property | Type | Description | Defined By |
---|---|---|---|
$db | yii\db\Connection | The database connection. | yii\db\QueryBuilder |
$separator | string | The separator between different fragments of a SQL statement. | yii\db\QueryBuilder |
$typeMap | array | The abstract column types mapped to physical column types. | yii\db\QueryBuilder |
Property | Type | Description | Defined By |
---|---|---|---|
$conditionBuilders | array | Map of query condition to builder methods. | yii\db\QueryBuilder |
$likeEscapeCharacter | string|null | Character used to escape special characters in LIKE conditions. | yii\db\QueryBuilder |
$likeEscapingReplacements | array | Map of chars to their replacements in LIKE conditions. | yii\db\QueryBuilder |
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\Object |
__construct() | Constructor. | yii\db\QueryBuilder |
__get() | Returns the value of an object property. | yii\base\Object |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Object |
__set() | Sets value of an object property. | yii\base\Object |
__unset() | Sets an object property to null. | yii\base\Object |
addColumn() | Builds a SQL statement for adding a new DB column. | yii\db\QueryBuilder |
addCommentOnColumn() | Builds a SQL command for adding comment to column | yii\db\QueryBuilder |
addCommentOnTable() | Builds a SQL command for adding comment to table | yii\db\QueryBuilder |
addForeignKey() | Builds a SQL statement for adding a foreign key constraint to an existing table. | yii\db\QueryBuilder |
addPrimaryKey() | Builds a SQL statement for adding a primary key constraint to an existing table. | yii\db\QueryBuilder |
alterColumn() | Builds a SQL statement for changing the definition of a column. | yii\db\QueryBuilder |
batchInsert() | Generates a batch INSERT SQL statement. | yii\db\QueryBuilder |
build() | Generates a SELECT SQL statement from a yii\db\Query object. | yii\db\QueryBuilder |
buildAndCondition() | Connects two or more SQL expressions with the AND or OR operator. | yii\db\QueryBuilder |
buildBetweenCondition() | Creates an SQL expressions with the BETWEEN operator. | yii\db\QueryBuilder |
buildColumns() | Processes columns and properly quotes them if necessary. | yii\db\QueryBuilder |
buildCondition() | Parses the condition specification and generates the corresponding SQL expression. | yii\db\QueryBuilder |
buildExistsCondition() | Creates an SQL expressions with the EXISTS operator. | yii\db\QueryBuilder |
buildFrom() | yii\db\QueryBuilder | |
buildGroupBy() | yii\db\QueryBuilder | |
buildHashCondition() | Creates a condition based on column-value pairs. | yii\db\QueryBuilder |
buildHaving() | yii\db\QueryBuilder | |
buildInCondition() | Creates an SQL expressions with the IN operator. | yii\db\QueryBuilder |
buildJoin() | yii\db\QueryBuilder | |
buildLikeCondition() | Creates an SQL expressions with the LIKE operator. | yii\db\QueryBuilder |
buildLimit() | yii\db\QueryBuilder | |
buildNotCondition() | Inverts an SQL expressions with NOT operator. | yii\db\QueryBuilder |
buildOrderBy() | yii\db\QueryBuilder | |
buildOrderByAndLimit() | Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL. | yii\db\QueryBuilder |
buildSelect() | yii\db\QueryBuilder | |
buildSimpleCondition() | Creates an SQL expressions like "column" operator value . | yii\db\QueryBuilder |
buildUnion() | yii\db\QueryBuilder | |
buildWhere() | yii\db\QueryBuilder | |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\Object |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Object |
checkIntegrity() | Builds a SQL statement for enabling or disabling integrity check. | yii\db\QueryBuilder |
className() | Returns the fully qualified name of this class. | yii\base\Object |
createIndex() | Builds a SQL statement for creating a new index. | yii\db\QueryBuilder |
createTable() | Builds a SQL statement for creating a new DB table. | yii\db\QueryBuilder |
delete() | Creates a DELETE SQL statement. | yii\db\QueryBuilder |
dropColumn() | Builds a SQL statement for dropping a DB column. | yii\db\QueryBuilder |
dropCommentFromColumn() | Builds a SQL command for adding comment to column | yii\db\QueryBuilder |
dropCommentFromTable() | Builds a SQL command for adding comment to table | yii\db\QueryBuilder |
dropForeignKey() | Builds a SQL statement for dropping a foreign key constraint. | yii\db\QueryBuilder |
dropIndex() | Builds a SQL statement for dropping an index. | yii\db\QueryBuilder |
dropPrimaryKey() | Builds a SQL statement for removing a primary key constraint to an existing table. | yii\db\QueryBuilder |
dropTable() | Builds a SQL statement for dropping a DB table. | yii\db\QueryBuilder |
getColumnType() | Converts an abstract column type into a physical column type. | yii\db\QueryBuilder |
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\Object |
hasProperty() | Returns a value indicating whether a property is defined. | yii\base\Object |
init() | Initializes the object. | yii\base\Object |
insert() | Creates an INSERT SQL statement. | yii\db\QueryBuilder |
renameColumn() | Builds a SQL statement for renaming a column. | yii\db\QueryBuilder |
renameTable() | Builds a SQL statement for renaming a DB table. | yii\db\QueryBuilder |
resetSequence() | Creates a SQL statement for resetting the sequence value of a table's primary key. | yii\db\QueryBuilder |
selectExists() | Creates a SELECT EXISTS() SQL statement. | yii\db\QueryBuilder |
truncateTable() | Builds a SQL statement for truncating a DB table. | yii\db\QueryBuilder |
update() | Creates an UPDATE SQL statement. | yii\db\QueryBuilder |
Method | Description | Defined By |
---|---|---|
buildCompositeInCondition() | Builds SQL for IN condition | yii\db\QueryBuilder |
buildSubqueryInCondition() | Builds SQL for IN condition | yii\db\QueryBuilder |
hasLimit() | Checks to see if the given limit is effective. | yii\db\QueryBuilder |
hasOffset() | Checks to see if the given offset is effective. | yii\db\QueryBuilder |
prepareInsertSelectSubQuery() | Prepare select-subquery and field names for INSERT INTO . | yii\db\QueryBuilder |
Constant | Value | Description | Defined By |
---|---|---|---|
PARAM_PREFIX | ':qp' | The prefix for automatically generated query binding parameters. | yii\db\QueryBuilder |
Map of query condition to builder methods. These methods are used by buildCondition() to build SQL conditions from array syntax.
protected array $conditionBuilders = ['NOT' => 'buildNotCondition', 'AND' => 'buildAndCondition', 'OR' => 'buildAndCondition', 'BETWEEN' => 'buildBetweenCondition', 'NOT BETWEEN' => 'buildBetweenCondition', 'IN' => 'buildInCondition', 'NOT IN' => 'buildInCondition', 'LIKE' => 'buildLikeCondition', 'NOT LIKE' => 'buildLikeCondition', 'OR LIKE' => 'buildLikeCondition', 'OR NOT LIKE' => 'buildLikeCondition', 'EXISTS' => 'buildExistsCondition', 'NOT EXISTS' => 'buildExistsCondition']
The database connection.
public yii\db\Connection $db = null
Character used to escape special characters in LIKE conditions. By default it's assumed to be \
.
protected string|null $likeEscapeCharacter = null
Map of chars to their replacements in LIKE conditions. By default it's configured to escape %
, _
and \
with \
.
protected array $likeEscapingReplacements = ['%' => '\%', '_' => '\_', '\\' => '\\\\']
The separator between different fragments of a SQL statement. Defaults to an empty space. This is mainly used by build() when generating a SQL statement.
public string $separator = ' '
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 array $typeMap = []
Constructor.
public void __construct ( $connection, $config = [] ) | ||
---|---|---|
$connection | yii\db\Connection |
The database connection. |
$config | array |
Name-value pairs that will be used to initialize the object properties |
Builds a SQL statement for adding a new DB column.
public string addColumn ( $table, $column, $type ) | ||
---|---|---|
$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. |
Builds a SQL command for adding comment to column
public string addCommentOnColumn ( $table, $column, $comment ) | ||
---|---|---|
$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 |
Builds a SQL command for adding comment to table
public string addCommentOnTable ( $table, $comment ) | ||
---|---|---|
$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 |
Builds a SQL statement for adding a foreign key constraint to an existing table.
The method will properly quote the table and column names.
public string addForeignKey ( $name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null ) | ||
---|---|---|
$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. |
Builds a SQL statement for adding a primary key constraint to an existing table.
public string addPrimaryKey ( $name, $table, $columns ) | ||
---|---|---|
$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. |
Builds a SQL statement for changing the definition of a column.
public string alterColumn ( $table, $column, $type ) | ||
---|---|---|
$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. |
Generates a batch INSERT SQL statement.
For example,
$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 string batchInsert ( $table, $columns, $rows ) | ||
---|---|---|
$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 |
Generates a SELECT SQL statement from a yii\db\Query object.
public array build ( $query, $params = [] ) | ||
---|---|---|
$query | yii\db\Query |
The yii\db\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 |
Connects two or more SQL expressions with the AND
or OR
operator.
public string buildAndCondition ( $operator, $operands, &$params ) | ||
---|---|---|
$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 |
Creates an SQL expressions with the BETWEEN
operator.
public string buildBetweenCondition ( $operator, $operands, &$params ) | ||
---|---|---|
$operator | string |
The operator to use (e.g. |
$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 |
throws | yii\base\InvalidParamException |
if wrong number of operands have been given. |
Processes columns and properly quotes them if necessary.
It will join all columns into a string with comma as separators.
public string buildColumns ( $columns ) | ||
---|---|---|
$columns | string|array |
The columns to be processed |
return | string |
The processing result |
Builds SQL for IN condition
protected string buildCompositeInCondition ( $operator, $columns, $values, &$params ) | ||
---|---|---|
$operator | string | |
$columns | array|Traversable | |
$values | array | |
$params | array | |
return | string |
SQL |
Parses the condition specification and generates the corresponding SQL expression.
public string buildCondition ( $condition, &$params ) | ||
---|---|---|
$condition | string|array|yii\db\Expression |
The condition specification. Please refer to yii\db\Query::where() on how to specify a condition. |
$params | array |
The binding parameters to be populated |
return | string |
The generated SQL expression |
Creates an SQL expressions with the EXISTS
operator.
public string buildExistsCondition ( $operator, $operands, &$params ) | ||
---|---|---|
$operator | string |
The operator to use (e.g. |
$operands | array |
Contains only one element which is a yii\db\Query object representing the sub-query. |
$params | array |
The binding parameters to be populated |
return | string |
The generated SQL expression |
throws | yii\base\InvalidParamException |
if the operand is not a yii\db\Query object. |
public string buildFrom ( $tables, &$params ) | ||
---|---|---|
$tables | array | |
$params | array |
The binding parameters to be populated |
return | string |
The FROM clause built from yii\db\Query::$from. |
public string buildGroupBy ( $columns ) | ||
---|---|---|
$columns | array | |
return | string |
The GROUP BY clause |
Creates a condition based on column-value pairs.
public string buildHashCondition ( $condition, &$params ) | ||
---|---|---|
$condition | array |
The condition specification. |
$params | array |
The binding parameters to be populated |
return | string |
The generated SQL expression |
public string buildHaving ( $condition, &$params ) | ||
---|---|---|
$condition | string|array | |
$params | array |
The binding parameters to be populated |
return | string |
The HAVING clause built from yii\db\Query::$having. |
Creates an SQL expressions with the IN
operator.
public string buildInCondition ( $operator, $operands, &$params ) | ||
---|---|---|
$operator | string |
The operator to use (e.g. |
$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 |
$params | array |
The binding parameters to be populated |
return | string |
The generated SQL expression |
throws | yii\db\Exception |
if wrong number of operands have been given. |
public string buildJoin ( $joins, &$params ) | ||
---|---|---|
$joins | array | |
$params | array |
The binding parameters to be populated |
return | string |
The JOIN clause built from yii\db\Query::$join. |
throws | yii\db\Exception |
if the $joins parameter is not in proper format |
Creates an SQL expressions with the LIKE
operator.
public string buildLikeCondition ( $operator, $operands, &$params ) | ||
---|---|---|
$operator | string |
The operator to use (e.g. |
$operands | array |
An array of two or three operands
|
$params | array |
The binding parameters to be populated |
return | string |
The generated SQL expression |
throws | yii\base\InvalidParamException |
if wrong number of operands have been given. |
public string buildLimit ( $limit, $offset ) | ||
---|---|---|
$limit | integer | |
$offset | integer | |
return | string |
The LIMIT and OFFSET clauses |
Inverts an SQL expressions with NOT
operator.
public string buildNotCondition ( $operator, $operands, &$params ) | ||
---|---|---|
$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 |
throws | yii\base\InvalidParamException |
if wrong number of operands have been given. |
public string buildOrderBy ( $columns ) | ||
---|---|---|
$columns | array | |
return | string |
The ORDER BY clause built from yii\db\Query::$orderBy. |
Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL.
public string buildOrderByAndLimit ( $sql, $orderBy, $limit, $offset ) | ||
---|---|---|
$sql | string |
The existing SQL (without ORDER BY/LIMIT/OFFSET) |
$orderBy | array |
The order by columns. See yii\db\Query::orderBy() for more details on how to specify this parameter. |
$limit | integer |
The limit number. See yii\db\Query::limit() for more details. |
$offset | integer |
The offset number. See yii\db\Query::offset() for more details. |
return | string |
The SQL completed with ORDER BY/LIMIT/OFFSET (if any) |
public string buildSelect ( $columns, &$params, $distinct = false, $selectOption = null ) | ||
---|---|---|
$columns | array | |
$params | array |
The binding parameters to be populated |
$distinct | boolean | |
$selectOption | string | |
return | string |
The SELECT clause built from yii\db\Query::$select. |
Creates an SQL expressions like "column" operator value
.
public string buildSimpleCondition ( $operator, $operands, &$params ) | ||
---|---|---|
$operator | string |
The operator to use. Anything could be used e.g. |
$operands | array |
Contains two column names. |
$params | array |
The binding parameters to be populated |
return | string |
The generated SQL expression |
throws | yii\base\InvalidParamException |
if wrong number of operands have been given. |
Builds SQL for IN condition
protected string buildSubqueryInCondition ( $operator, $columns, $values, &$params ) | ||
---|---|---|
$operator | string | |
$columns | array | |
$values | yii\db\Query | |
$params | array | |
return | string |
SQL |
public string buildUnion ( $unions, &$params ) | ||
---|---|---|
$unions | array | |
$params | array |
The binding parameters to be populated |
return | string |
The UNION clause built from yii\db\Query::$union. |
public string buildWhere ( $condition, &$params ) | ||
---|---|---|
$condition | string|array | |
$params | array |
The binding parameters to be populated |
return | string |
The WHERE clause built from yii\db\Query::$where. |
Builds a SQL statement for enabling or disabling integrity check.
public string checkIntegrity ( $check = true, $schema = '', $table = '' ) | ||
---|---|---|
$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 |
throws | yii\base\NotSupportedException |
if this is not supported by the underlying DBMS |
Builds a SQL statement for creating a new index.
public string createIndex ( $name, $table, $columns, $unique = false ) | ||
---|---|---|
$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. |
Builds a SQL statement for creating a new DB table.
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 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,
$sql = $queryBuilder->createTable('user', [ 'id' => 'pk', 'name' => 'string', 'age' => 'integer', ]);
public string createTable ( $table, $columns, $options = null ) | ||
---|---|---|
$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. |
Creates a DELETE SQL statement.
For example,
$sql = $queryBuilder->delete('user', 'status = 0');
The method will properly escape the table and column names.
public string delete ( $table, $condition, &$params ) | ||
---|---|---|
$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 yii\db\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 |
Builds a SQL statement for dropping a DB column.
public string dropColumn ( $table, $column ) | ||
---|---|---|
$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. |
Builds a SQL command for adding comment to column
public string dropCommentFromColumn ( $table, $column ) | ||
---|---|---|
$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 |
Builds a SQL command for adding comment to table
public string dropCommentFromTable ( $table ) | ||
---|---|---|
$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 |
Builds a SQL statement for dropping a foreign key constraint.
public string dropForeignKey ( $name, $table ) | ||
---|---|---|
$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. |
Builds a SQL statement for dropping an index.
public string dropIndex ( $name, $table ) | ||
---|---|---|
$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. |
Builds a SQL statement for removing a primary key constraint to an existing table.
public string dropPrimaryKey ( $name, $table ) | ||
---|---|---|
$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. |
Builds a SQL statement for dropping a DB table.
public string dropTable ( $table ) | ||
---|---|---|
$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. |
Converts an abstract column type into a physical column type.
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 string getColumnType ( $type ) | ||
---|---|---|
$type | string|yii\db\ColumnSchemaBuilder |
Abstract column type |
return | string |
Physical column type. |
Checks to see if the given limit is effective.
protected boolean hasLimit ( $limit ) | ||
---|---|---|
$limit | mixed |
The given limit |
return | boolean |
Whether the limit is effective |
Checks to see if the given offset is effective.
protected boolean hasOffset ( $offset ) | ||
---|---|---|
$offset | mixed |
The given offset |
return | boolean |
Whether the offset is effective |
Creates an INSERT SQL statement.
For example,
$sql = $queryBuilder->insert('user', [ 'name' => 'Sam', 'age' => 30, ], $params);
The method will properly escape the table and column names.
public string insert ( $table, $columns, &$params ) | ||
---|---|---|
$table | string |
The table that new rows will be inserted into. |
$columns | array|yii\db\Query |
The column data (name => value) to be inserted into the table or instance of \yii\db\yii\db\Query to perform INSERT INTO ... SELECT SQL statement. Passing of \yii\db\yii\db\Query is available since version 2.0.11. |
$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 |
Prepare select-subquery and field names for INSERT INTO .
.. SELECT SQL statement.
protected array prepareInsertSelectSubQuery ( $columns, $schema, $params = [] ) | ||
---|---|---|
$columns | yii\db\Query |
Object, which represents select query. |
$schema | yii\db\Schema |
Schema object to quote column name. |
$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. |
throws | yii\base\InvalidParamException |
if query's select does not contain named parameters only. |
Builds a SQL statement for renaming a column.
public string renameColumn ( $table, $oldName, $newName ) | ||
---|---|---|
$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. |
Builds a SQL statement for renaming a DB table.
public string renameTable ( $oldName, $newName ) | ||
---|---|---|
$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. |
Creates a SQL statement for resetting the sequence value of a table's primary key.
The sequence will be reset such that the primary key of the next new row inserted will have the specified value or 1.
public string resetSequence ( $table, $value = null ) | ||
---|---|---|
$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 |
throws | yii\base\NotSupportedException |
if this is not supported by the underlying DBMS |
Creates a SELECT EXISTS() SQL statement.
public string selectExists ( $rawSql ) | ||
---|---|---|
$rawSql | string |
The subquery in a raw form to select from. |
return | string |
The SELECT EXISTS() SQL statement. |
Builds a SQL statement for truncating a DB table.
public string truncateTable ( $table ) | ||
---|---|---|
$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. |
Creates an UPDATE SQL statement.
For example,
$params = []; $sql = $queryBuilder->update('user', ['status' => 1], 'age > 30', $params);
The method will properly escape the table and column names.
public string update ( $table, $columns, $condition, &$params ) | ||
---|---|---|
$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 yii\db\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 |
© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc-2.0/yii-db-querybuilder.html