QueryBuilder ​
- Type
- Class
- Namespace
- craft\db\mysql
- Inherits
- craft\db\mysql\QueryBuilder » yii\db\mysql\QueryBuilder » yii\db\QueryBuilder » yii\base\BaseObject
- Implements
- yii\base\Configurable
- Since
- 3.0.0
Public Properties ​
Property | Description |
---|---|
db | craft\db\Connection – Connection the DB connection that this command is associated with. |
separator | string – The separator between different fragments of a SQL statement. |
typeMap | array – Mapping from abstract column types (keys) to physical column types (values). |
db
​
- Type
- craft\db\Connection
- Default value
null
Connection the DB connection that this command is associated with.
Protected Properties ​
Property | Description |
---|---|
conditionBuilders | array – Map of query condition to builder methods. |
conditionClasses | array – Map of condition aliases to condition classes. |
expressionBuilders | string[], yii\db\ExpressionBuilderInterface – Maps expression class to expression builder class. |
Public Methods ​
Method | Description |
---|---|
__call() | Calls the named method which is not a class method. |
__construct() | Constructor. |
__get() | Returns the value of an object property. |
__isset() | Checks if a property is set, i.e. defined and not null. |
__set() | Sets value of an object property. |
__unset() | Sets an object property to null. |
addCheck() | Creates a SQL command for adding a check constraint to an existing table. |
addColumn() | Builds a SQL statement for adding a new DB column. |
addCommentOnColumn() | Builds a SQL command for adding comment to column. |
addCommentOnTable() | Builds a SQL command for adding comment to table. |
addDefaultValue() | Creates a SQL command for adding a default value constraint to an existing table. |
addForeignKey() | Builds a SQL statement for adding a foreign key constraint to an existing table. |
addPrimaryKey() | Builds a SQL statement for adding a primary key constraint to an existing table. |
addUnique() | Creates a SQL command for adding an unique constraint to an existing table. |
alterColumn() | Builds a SQL statement for changing the definition of a column. |
batchInsert() | Generates a batch INSERT SQL statement. |
bindParam() | Helper method to add $value to $params array using PARAM_PREFIX. |
build() | Generates a SELECT SQL statement from a yii\db\Query object. |
buildAndCondition() | Connects two or more SQL expressions with the AND or OR operator. |
buildBetweenCondition() | Creates an SQL expressions with the BETWEEN operator. |
buildColumns() | Processes columns and properly quotes them if necessary. |
buildCondition() | Parses the condition specification and generates the corresponding SQL expression. |
buildExistsCondition() | Creates an SQL expressions with the EXISTS operator. |
buildExpression() | Builds given $expression |
buildFrom() | |
buildGroupBy() | |
buildHashCondition() | Creates a condition based on column-value pairs. |
buildHaving() | |
buildInCondition() | Creates an SQL expressions with the IN operator. |
buildJoin() | |
buildLikeCondition() | Creates an SQL expressions with the LIKE operator. |
buildLimit() | |
buildNotCondition() | Inverts an SQL expressions with NOT operator. |
buildOrderBy() | |
buildOrderByAndLimit() | Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL. |
buildSelect() | |
buildSimpleCondition() | Creates an SQL expressions like "column" operator value . |
buildUnion() | |
buildWhere() | |
buildWithQueries() | |
canGetProperty() | Returns a value indicating whether a property can be read. |
canSetProperty() | Returns a value indicating whether a property can be set. |
checkIntegrity() | Builds a SQL statement for enabling or disabling integrity check. |
className() | Returns the fully qualified name of this class. |
createConditionFromArray() | Transforms $condition defined in array format (as described in yii\db\Query::where() to instance of \yii\db\yii\db\condition\ConditionInterface according to conditionClasses map. |
createIndex() | Builds a SQL statement for creating a new index. |
createTable() | Builds a SQL statement for creating a new DB table. |
createView() | Creates a SQL View. |
delete() | Creates a DELETE SQL statement. |
deleteDuplicates() | Builds the SQL expression used to delete duplicate rows from a table. |
dropCheck() | Creates a SQL command for dropping a check constraint. |
dropColumn() | Builds a SQL statement for dropping a DB column. |
dropCommentFromColumn() | Builds a SQL command for adding comment to column. |
dropCommentFromTable() | Builds a SQL command for adding comment to table. |
dropDefaultValue() | Creates a SQL command for dropping a default value constraint. |
dropForeignKey() | Builds a SQL statement for dropping a foreign key constraint. |
dropIndex() | Builds a SQL statement for dropping an index. |
dropPrimaryKey() | Builds a SQL statement for removing a primary key constraint to an existing table. |
dropTable() | Builds a SQL statement for dropping a DB table. |
dropTableIfExists() | Builds a SQL statement for dropping a DB table if it exists. |
dropUnique() | Creates a SQL command for dropping an unique constraint. |
dropView() | Drops a SQL View. |
executeResetSequence() | Execute a SQL statement for resetting the sequence value of a table's primary key. |
fixedOrder() | Builds the SQL expression used to return a DB result in a fixed order. |
getColumnType() | Converts an abstract column type into a physical column type. |
getExpressionBuilder() | Gets object of yii\db\ExpressionBuilderInterface that is suitable for $expression. |
hasMethod() | Returns a value indicating whether a method is defined. |
hasProperty() | Returns a value indicating whether a property is defined. |
init() | Initializes the object. |
insert() | Creates an INSERT SQL statement. |
renameColumn() | Builds a SQL statement for renaming a column. |
renameSequence() | Builds a SQL statement for renaming a DB sequence. |
renameTable() | Builds a SQL statement for renaming a DB table. |
replace() | Builds a SQL statement for replacing some text with other text in a given table column. |
resetSequence() | Creates a SQL statement for resetting the sequence value of a table's primary key. |
selectExists() | Creates a SELECT EXISTS() SQL statement. |
setConditionClasses() | Setter for conditionClasses property. |
setExpressionBuilders() | Setter for expressionBuilders property. |
truncateTable() | Builds a SQL statement for truncating a DB table. |
update() | Creates an UPDATE SQL statement. |
upsert() | Creates an SQL statement to insert rows into a database table if they do not already exist (matching unique constraints), or update them if they do. |
createTable()
​
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',
]);
Arguments ​
$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, null) – Additional SQL fragment that will be appended to the generated SQL.
Returns ​
string – The SQL statement for creating a new DB table.
deleteDuplicates()
​
- Since
- 3.5.2
Builds the SQL expression used to delete duplicate rows from a table.
Arguments ​
$table
(string) – The table to be updated.$columns
(string[]) – The column names that contain duplicate data$pk
(string) – The primary key column name
Returns ​
string – The SQL expression
dropTableIfExists()
​
Builds a SQL statement for dropping a DB table if it exists.
Arguments ​
$table
(string) – The table to be dropped. The name will be properly quoted by the method.
Returns ​
string – The SQL statement for dropping a DB table.
fixedOrder()
​
Builds the SQL expression used to return a DB result in a fixed order.
Arguments ​
$column
(string) – The column name that contains the values.$values
(array) – The column values, in the order in which the rows should be returned in.
Returns ​
string – The SQL expression.
init()
​
Initializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
renameSequence()
​
Builds a SQL statement for renaming a DB sequence.
Arguments ​
$oldName
(string) – The sequence to be renamed. The name will be properly quoted by the method.$newName
(string) – The new sequence name. The name will be properly quoted by the method.
Returns ​
string – The SQL statement for renaming a DB table.
Throws ​
replace()
​
Builds a SQL statement for replacing some text with other text in a given table column.
Arguments ​
$table
(string) – The table to be updated.$column
(string) – The column to be searched.$find
(string) – The text to be searched for.$replace
(string) – The replacement text.$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 generated by this method. They should be bound to the DB command later.
Returns ​
string – The SQL statement for replacing some text in a given table.
Protected Methods ​
Method | Description |
---|---|
defaultConditionClasses() | Contains array of default condition classes. Extend this method, if you want to change default condition classes for the query builder. See conditionClasses docs for details. |
defaultExpressionBuilders() | Contains array of default expression builders. Extend this method and override it, if you want to change default expression builders for this query builder. See expressionBuilders docs for details. |
extractAlias() | Extracts table alias if there is one or returns false |
hasLimit() | Checks to see if the given limit is effective. |
hasOffset() | Checks to see if the given offset is effective. |
prepareInsertSelectSubQuery() | Prepare select-subquery and field names for INSERT INTO ... SELECT SQL statement. |
prepareInsertValues() | Prepares a VALUES part for an INSERT SQL statement. |
prepareUpdateSets() | Prepares a SET parts for an UPDATE SQL statement. |
prepareUpsertColumns() |
Constants ​
Constant | Description |
---|---|
PARAM_PREFIX | The prefix for automatically generated query binding parameters. |