Craft 3 Class Reference

Class craft\db\Command

Inheritance
craft\db\Command » yii\db\Command » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Available since version
3.0
Source Code
https://github.com/craftcms/cms/blob/master/src/db/Command.php

Public Properties
Property Type Description Defined By
$db yii\db\Connection The DB connection that this command is associated with yii\db\Command
$fetchMode integer The default fetch mode for this command. yii\db\Command
$params array The parameters (name => value) that are bound to the current PDO statement. yii\db\Command
$pdoStatement PDOStatement The PDOStatement object that this command is associated with yii\db\Command
$queryCacheDependency yii\caching\Dependency The dependency to be associated with the cached query result for this command yii\db\Command
$queryCacheDuration integer The default number of seconds that query results can remain valid in cache. yii\db\Command
Public Methods
Method Description Defined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
addCheck() Creates a SQL command for adding a check constraint to an existing table. yii\db\Command
addColumn() Creates a SQL command for adding a new DB column. yii\db\Command
addCommentOnColumn() Builds a SQL command for adding comment to column. yii\db\Command
addCommentOnTable() Builds a SQL command for adding comment to table. yii\db\Command
addDefaultValue() Creates a SQL command for adding a default value constraint to an existing table. yii\db\Command
addForeignKey() Creates a SQL command for adding a foreign key constraint to an existing table. yii\db\Command
addPrimaryKey() Creates a SQL command for adding a primary key constraint to an existing table. yii\db\Command
addUnique() Creates a SQL command for adding an unique constraint to an existing table. yii\db\Command
alterColumn() Creates a SQL command for changing the definition of a column. yii\db\Command
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
batchInsert() Creates a batch INSERT command. craft\db\Command
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
bindParam() Binds a parameter to the SQL statement to be executed. yii\db\Command
bindValue() Binds a value to a parameter. yii\db\Command
bindValues() Binds a list of values to the corresponding parameters. yii\db\Command
cache() Enables query cache for this command. yii\db\Command
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
cancel() Cancels the execution of the SQL statement. yii\db\Command
checkIntegrity() Builds a SQL command for enabling or disabling integrity check. yii\db\Command
className() Returns the fully qualified name of this class. yii\base\BaseObject
createIndex() Creates a SQL command for creating a new index. yii\db\Command
createTable() Creates a SQL command for creating a new DB table. yii\db\Command
createView() Creates a SQL View. yii\db\Command
delete() Creates a DELETE command. yii\db\Command
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
dropCheck() Creates a SQL command for dropping a check constraint. yii\db\Command
dropColumn() Creates a SQL command for dropping a DB column. yii\db\Command
dropCommentFromColumn() Builds a SQL command for dropping comment from column. yii\db\Command
dropCommentFromTable() Builds a SQL command for dropping comment from table. yii\db\Command
dropDefaultValue() Creates a SQL command for dropping a default value constraint. yii\db\Command
dropForeignKey() Creates a SQL command for dropping a foreign key constraint. yii\db\Command
dropIndex() Creates a SQL command for dropping an index. yii\db\Command
dropPrimaryKey() Creates a SQL command for removing a primary key constraint to an existing table. yii\db\Command
dropTable() Creates a SQL command for dropping a DB table. yii\db\Command
dropTableIfExists() Creates a SQL statement for dropping a DB table, if it exists. craft\db\Command
dropUnique() Creates a SQL command for dropping an unique constraint. yii\db\Command
dropView() Drops a SQL View. yii\db\Command
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
execute() Executes the SQL statement. yii\db\Command
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getRawSql() Returns the raw SQL by inserting parameter values into the corresponding placeholders in \yii\db\sql. yii\db\Command
getSql() Returns the SQL statement for this command. yii\db\Command
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. yii\base\BaseObject
insert() Creates an INSERT command. craft\db\Command
noCache() Disables query cache for this command. yii\db\Command
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
prepare() Prepares the SQL statement to be executed. yii\db\Command
query() Executes the SQL statement and returns query result. yii\db\Command
queryAll() Executes the SQL statement and returns ALL rows at once. yii\db\Command
queryColumn() Executes the SQL statement and returns the first column of the result. yii\db\Command
queryOne() Executes the SQL statement and returns the first row of the result. yii\db\Command
queryScalar() Executes the SQL statement and returns the value of the first column in the first row of data. yii\db\Command
renameColumn() Creates a SQL command for renaming a column. yii\db\Command
renameSequence() Creates a SQL statement for renaming a DB sequence. craft\db\Command
renameTable() Creates a SQL command for renaming a DB table. yii\db\Command
replace() Creates a SQL statement for replacing some text with other text in a given table column. craft\db\Command
resetSequence() Creates a SQL command for resetting the sequence value of a table's primary key. yii\db\Command
setRawSql() Specifies the SQL statement to be executed. The SQL statement will not be modified in any way. yii\db\Command
setSql() Specifies the SQL statement to be executed. The SQL statement will be quoted using yii\db\Connection::quoteSql(). yii\db\Command
trigger() Triggers an event. yii\base\Component
truncateTable() Creates a SQL command for truncating a DB table. yii\db\Command
update() Creates an UPDATE command. craft\db\Command
upsert() Creates a command to insert rows into a database table if they do not already exist (matching unique constraints), or update them if they do. craft\db\Command
Protected Methods
Method Description Defined By
bindPendingParams() Binds pending parameters that were registered via bindValue() and bindValues(). yii\db\Command
internalExecute() Executes a prepared statement. yii\db\Command
queryInternal() Performs the actual DB query of a SQL statement. yii\db\Command
refreshTableSchema() Refreshes table schema, which was marked by requireTableSchemaRefresh(). yii\db\Command
requireTableSchemaRefresh() Marks a specified table schema to be refreshed after command execution. yii\db\Command
requireTransaction() Marks the command to be executed in transaction. yii\db\Command
reset() Resets command properties to their initial state. yii\db\Command
setRetryHandler() Sets a callable (e.g. anonymous function) that is called when yii\db\Exception is thrown when executing the command. The signature of the callable should be: yii\db\Command

Method Details

batchInsert() public method #

Creates a batch INSERT command.

For example,

$connection->createCommand()->batchInsert('user', ['name', 'age'], [
    ['Tom', 30],
    ['Jane', 20],
    ['Linda', 25],
])->execute();

The method will properly escape the column names, and quote the values to be inserted.

Note that the values in each row must match the corresponding column names.

Also note that the created command is not executed until execute() is called.

public static batchInsert ( $table, $columns, $rows, $includeAuditColumns true )
$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.
$includeAuditColumns boolean Whether dateCreated, dateUpdated, and uid values should be added to $columns.
return static The command object itself.

dropTableIfExists() public method #

Creates a SQL statement for dropping a DB table, if it exists.

public craft\db\Command dropTableIfExists ( \craft\db\string $table )
$table string The table to be dropped. The name will be properly quoted by the method.
return craft\db\Command The command object itself

insert() public method #

Creates an INSERT command.

For example,

$connection->createCommand()->insert('user', [
    'name' => 'Sam',
    'age' => 30,
])->execute();

The method will properly escape the column names, and bind the values to be inserted.

Note that the created command is not executed until execute() is called.

public static insert ( $table, $columns, $includeAuditColumns true )
$table string The table that new rows will be inserted into.
$columns array The column data (name => value) to be inserted into the table.
$includeAuditColumns boolean Whether dateCreated, dateUpdated, and uid values should be added to $columns.
return static The command object itself

renameSequence() public method #

Creates a SQL statement for renaming a DB sequence.

public craft\db\Command renameSequence ( \craft\db\string $oldName, \craft\db\string $newName )
$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.
return craft\db\Command The command object itself

replace() public method #

Creates a SQL statement for replacing some text with other text in a given table column.

public craft\db\Command replace ( \craft\db\string $table, \craft\db\string $column, \craft\db\string $find, \craft\db\string $replace, $condition '', array $params = [] )
$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 string, array The condition that will be put in the WHERE part. Please refer to craft\db\Query::where() on how to specify condition.
$params array The parameters to be bound to the command.
return craft\db\Command The command object itself.

update() public method #

Creates an UPDATE command.

For example,

$connection->createCommand()->update('user', ['status' => 1], 'age > 30')->execute();

or with using parameter binding for the condition:

$minAge = 30;
$connection->createCommand()->update('user', ['status' => 1], 'age > :minAge', [':minAge' => $minAge])->execute();

The method will properly escape the column names and bind the values to be updated.

Note that the created command is not executed until execute() is called.

public static update ( $table, $columns, $condition '', $params = [], $includeAuditColumns true )
$table string The table to be updated.
$columns array The column data (name => value) to be updated.
$condition string, array The condition that will be put in the WHERE part. Please refer to craft\db\Query::where() on how to specify condition.
$params array The parameters to be bound to the command.
$includeAuditColumns boolean Whether the dateUpdated value should be added to $columns.
return static The command object itself.

upsert() public method #

Creates a command to insert rows into a database table if they do not already exist (matching unique constraints), or update them if they do.

For example,

$sql = $queryBuilder->upsert('pages', [
    'name' => 'Front page',
    'url' => 'http://example.com/', // url is unique
    'visits' => 0,
], [
    'visits' => new \yii\db\Expression('visits + 1'),
], $params);

The method will properly escape the table and column names.

public $this upsert ( $table, $insertColumns, $updateColumns true, $params = [], \craft\db\bool $includeAuditColumns true )
$table string The table that new rows will be inserted into/updated in.
$insertColumns array, craft\db\Query The column data (name => value) to be inserted into the table or instance of craft\db\Query to perform INSERT INTO ... SELECT SQL statement.
$updateColumns array, boolean The column data (name => value) to be updated if they already exist. If true is passed, the column data will be updated to match the insert column data. If false is passed, no update will be performed if the column data already exists.
$params array The parameters to be bound to the command.
$includeAuditColumns boolean Whether dateCreated, dateUpdated, and uid values should be added to $columns.
return $this The command object itself.