W3cubDocs

/Yii 2.0

Class yii\db\mysql\QueryBuilder

Inheritance yii\db\mysql\QueryBuilder » yii\db\QueryBuilder » yii\base\Object
Implements yii\base\Configurable
Available since version 2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/db/mysql/QueryBuilder.php

QueryBuilder is the query builder for MySQL databases.

Public Properties

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 Mapping from abstract column types (keys) to physical column types (values). yii\db\mysql\QueryBuilder

Protected Properties

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

Public Methods

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\mysql\QueryBuilder
addCommentOnTable() Builds a SQL command for adding comment to table yii\db\mysql\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\mysql\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\mysql\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\mysql\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\mysql\QueryBuilder
dropCommentFromTable() Builds a SQL command for adding comment to table yii\db\mysql\QueryBuilder
dropForeignKey() Builds a SQL statement for dropping a foreign key constraint. yii\db\mysql\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\mysql\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\mysql\QueryBuilder
renameColumn() Builds a SQL statement for renaming a column. yii\db\mysql\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\mysql\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

Protected Methods

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\mysql\QueryBuilder
hasOffset() Checks to see if the given offset is effective. yii\db\mysql\QueryBuilder
prepareInsertSelectSubQuery() Prepare select-subquery and field names for INSERT INTO . yii\db\QueryBuilder

Constants

Constant Value Description Defined By
PARAM_PREFIX ':qp' The prefix for automatically generated query binding parameters. yii\db\QueryBuilder

Property Details

$typeMap public property

Mapping from abstract column types (keys) to physical column types (values).

public array $typeMap = [\yii\db\mysql\Schema::TYPE_PK => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', \yii\db\mysql\Schema::TYPE_UPK => 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', \yii\db\mysql\Schema::TYPE_BIGPK => 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', \yii\db\mysql\Schema::TYPE_UBIGPK => 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', \yii\db\mysql\Schema::TYPE_CHAR => 'char(1)', \yii\db\mysql\Schema::TYPE_STRING => 'varchar(255)', \yii\db\mysql\Schema::TYPE_TEXT => 'text', \yii\db\mysql\Schema::TYPE_SMALLINT => 'smallint(6)', \yii\db\mysql\Schema::TYPE_INTEGER => 'int(11)', \yii\db\mysql\Schema::TYPE_BIGINT => 'bigint(20)', \yii\db\mysql\Schema::TYPE_FLOAT => 'float', \yii\db\mysql\Schema::TYPE_DOUBLE => 'double', \yii\db\mysql\Schema::TYPE_DECIMAL => 'decimal(10,0)', \yii\db\mysql\Schema::TYPE_DATETIME => 'datetime', \yii\db\mysql\Schema::TYPE_TIMESTAMP => 'timestamp', \yii\db\mysql\Schema::TYPE_TIME => 'time', \yii\db\mysql\Schema::TYPE_DATE => 'date', \yii\db\mysql\Schema::TYPE_BINARY => 'blob', \yii\db\mysql\Schema::TYPE_BOOLEAN => 'tinyint(1)', \yii\db\mysql\Schema::TYPE_MONEY => 'decimal(19,4)']

Method Details

addCommentOnColumn() public method (available since version 2.0.8)

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

addCommentOnTable() public method (available since version 2.0.8)

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

buildLimit() public method

public string buildLimit ( $limit, $offset )
$limit integer
$offset integer
return string

The LIMIT and OFFSET clauses

checkIntegrity() public method

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. Meaningless for MySQL.

$table string

The table name. Meaningless for MySQL.

return string

The SQL statement for checking integrity

createIndex() public method

Builds a SQL statement for creating a new index.

See also https://bugs.mysql.com/bug.php?id=48875.

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.

dropCommentFromColumn() public method (available since version 2.0.8)

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

dropCommentFromTable() public method (available since version 2.0.8)

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

dropForeignKey() public method

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.

dropPrimaryKey() public method

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.

hasLimit() protected method

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

hasOffset() protected method

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

insert() public method

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\mysql\yii\db\Query to perform INSERT INTO ... SELECT SQL statement. Passing of \yii\db\mysql\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

renameColumn() public method

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.

throws yii\db\Exception

resetSequence() public method

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 ( $tableName, $value = null )
$tableName string

The name of the table whose primary key sequence will be reset

$value mixed

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\InvalidParamException

if the table does not exist or there is no sequence associated with the table.

© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc-2.0/yii-db-mysql-querybuilder.html