0 follower

Interface Yiisoft\Db\Command\CommandInterface

Implemented byYiisoft\Db\Command\AbstractCommand, Yiisoft\Db\Debug\CommandInterfaceProxy, Yiisoft\Db\Driver\Pdo\AbstractPdoCommand, Yiisoft\Db\Driver\Pdo\PdoCommandInterface

This interface represents a database command, such as a SELECT, INSERT, UPDATE, or DELETE statement.

A command instance is usually created by calling {@see \Yiisoft\Db\Connection\ConnectionInterface::createCommand}.

Public Methods

Hide inherited methods

Method Description Defined By
addCheck() Creates an SQL command for adding a CHECK constraint to an existing table. Yiisoft\Db\Command\CommandInterface
addColumn() Creates an SQL command for adding a new DB column. Yiisoft\Db\Command\CommandInterface
addCommentOnColumn() Builds an SQL command for adding a comment to a column. Yiisoft\Db\Command\CommandInterface
addCommentOnTable() Builds an SQL command for adding comment to the table. Yiisoft\Db\Command\CommandInterface
addDefaultValue() Creates an SQL command for adding a default value constraint to an existing table. Yiisoft\Db\Command\CommandInterface
addForeignKey() Creates an SQL command for adding a foreign key constraint to an existing table. Yiisoft\Db\Command\CommandInterface
addPrimaryKey() Creates an SQL command for adding a primary key constraint to an existing table. Yiisoft\Db\Command\CommandInterface
addUnique() Creates an SQL command for adding a unique constraint to an existing table. Yiisoft\Db\Command\CommandInterface
alterColumn() Creates an SQL command for changing the definition of a column. Yiisoft\Db\Command\CommandInterface
bindParam() Binds a parameter to the SQL statement to be executed. Yiisoft\Db\Command\CommandInterface
bindValue() Binds a value to a parameter. Yiisoft\Db\Command\CommandInterface
bindValues() Binds a list of values to the corresponding parameters. Yiisoft\Db\Command\CommandInterface
cancel() Cancels the execution of the SQL statement. Yiisoft\Db\Command\CommandInterface
checkIntegrity() Builds an SQL command for enabling or disabling integrity check. Yiisoft\Db\Command\CommandInterface
createIndex() Creates an SQL command for creating a new index. Yiisoft\Db\Command\CommandInterface
createTable() Creates an SQL command for creating a new DB table. Yiisoft\Db\Command\CommandInterface
createView() Creates a SQL View. Yiisoft\Db\Command\CommandInterface
delete() Creates a DELETE command. Yiisoft\Db\Command\CommandInterface
dropCheck() Creates an SQL command for dropping a check constraint. Yiisoft\Db\Command\CommandInterface
dropColumn() Creates an SQL command for dropping a DB column. Yiisoft\Db\Command\CommandInterface
dropCommentFromColumn() Builds an SQL command for dropping comment from column. Yiisoft\Db\Command\CommandInterface
dropCommentFromTable() Builds an SQL command for dropping comment from the table. Yiisoft\Db\Command\CommandInterface
dropDefaultValue() Creates an SQL command for dropping a default value constraint. Yiisoft\Db\Command\CommandInterface
dropForeignKey() Creates an SQL command for dropping a foreign key constraint. Yiisoft\Db\Command\CommandInterface
dropIndex() Creates an SQL command for dropping an index. Yiisoft\Db\Command\CommandInterface
dropPrimaryKey() Creates an SQL command for removing a primary key constraint to an existing table. Yiisoft\Db\Command\CommandInterface
dropTable() Creates an SQL command for dropping a DB table. Yiisoft\Db\Command\CommandInterface
dropUnique() Creates an SQL command for dropping a unique constraint. Yiisoft\Db\Command\CommandInterface
dropView() Drops an SQL View. Yiisoft\Db\Command\CommandInterface
execute() Executes the SQL statement. Yiisoft\Db\Command\CommandInterface
getParams() Return the params used in the last query. Yiisoft\Db\Command\CommandInterface
getRawSql() Returns the raw SQL by inserting parameter values into the corresponding placeholders in {@see getSql()}. Yiisoft\Db\Command\CommandInterface
getSql() Returns the SQL statement for this command. Yiisoft\Db\Command\CommandInterface
insert() Creates an INSERT command. Yiisoft\Db\Command\CommandInterface
insertBatch() Creates a batch INSERT command. Yiisoft\Db\Command\CommandInterface
insertReturningPks() Attention! Please use function only as a last resort. The feature will be refactored in future releases. Yiisoft\Db\Command\CommandInterface
prepare() Prepares the SQL statement to be executed. Yiisoft\Db\Command\CommandInterface
query() Executes the SQL statement and returns a query result. Yiisoft\Db\Command\CommandInterface
queryAll() Executes the SQL statement and returns ALL rows at once. Yiisoft\Db\Command\CommandInterface
queryColumn() Execute the SQL statement and returns the first column of the result. Yiisoft\Db\Command\CommandInterface
queryOne() Executes the SQL statement and returns the first row of the result. Yiisoft\Db\Command\CommandInterface
queryScalar() Execute the SQL statement and returns the value of the first column in the first row of data. Yiisoft\Db\Command\CommandInterface
renameColumn() Creates an SQL command for renaming a column. Yiisoft\Db\Command\CommandInterface
renameTable() Creates an SQL command for renaming a DB table. Yiisoft\Db\Command\CommandInterface
resetSequence() Executes a db command resetting the sequence value of a table's primary key. Yiisoft\Db\Command\CommandInterface
setRawSql() Specifies the SQL statement to execute. Yiisoft\Db\Command\CommandInterface
setRetryHandler() Sets a closure (anonymous function) which called when a database exception is thrown when executing the command. Yiisoft\Db\Command\CommandInterface
setSql() Specifies the SQL statement to execute. The SQL statement will be quoted using {@see ConnectionInterface::quoteSql()}. Yiisoft\Db\Command\CommandInterface
showDatabases() List all database names in the current connection. Yiisoft\Db\Command\CommandInterface
truncateTable() Creates an SQL command for truncating a DB table. Yiisoft\Db\Command\CommandInterface
update() Creates an UPDATE command. Yiisoft\Db\Command\CommandInterface
upsert() Creates a command to insert rows into a database table if they don't already exist (matching unique constraints) or update them if they do. Yiisoft\Db\Command\CommandInterface
upsertReturning() Creates a command to insert rows into a database table if they don't already exist (matching unique constraints) or update them if they do, returning values from the specified columns. Yiisoft\Db\Command\CommandInterface
upsertReturningPks() Creates a command to insert rows into a database table if they don't already exist (matching unique constraints) or update them if they do, with returning inserted or updated primary key values. Yiisoft\Db\Command\CommandInterface
withDbTypecasting() Returns copy of the instance with enabled or disabled typecasting of values when inserting or updating records. Yiisoft\Db\Command\CommandInterface
withPhpTypecasting() Returns a copy of the instance with enabled or disabled typecasting of values when retrieving records from DB. Yiisoft\Db\Command\CommandInterface
withTypecasting() Returns a copy of the instance with enabled or disabled typecasting of values when inserting, updating or retrieving records from DB. Yiisoft\Db\Command\CommandInterface

Method Details

Hide inherited methods

addCheck() public abstract method

Creates an SQL command for adding a CHECK constraint to an existing table.

public abstract Yiisoft\Db\Command\CommandInterface addCheck ( string $table, string $name, string $expression )
$table string

The name of the table to add check constraint to.

$name string

The name of the check constraint.

$expression string

The SQL of the CHECK constraint.

Note: The method will quote the name and table parameters before using them in the generated SQL.

                public function addCheck(string $table, string $name, string $expression): static;

            
addColumn() public abstract method

Creates an SQL command for adding a new DB column.

public abstract Yiisoft\Db\Command\CommandInterface addColumn ( string $table, string $column, Yiisoft\Db\Schema\Column\ColumnInterface|string $type )
$table string

The name of the table to add new column to.

$column string

The name of the new column.

$type Yiisoft\Db\Schema\Column\ColumnInterface|string

The column type. {@see \Yiisoft\Db\Command\QueryBuilder::buildColumnDefinition()} will be called to convert the given column type to the database one. For example, string will be converted to varchar(255), and string not null becomes varchar(255) not null.

Note: The method will quote the table and column parameters before using them in the generated SQL.

                public function addColumn(string $table, string $column, ColumnInterface|string $type): static;

            
addCommentOnColumn() public abstract method

Builds an SQL command for adding a comment to a column.

public abstract Yiisoft\Db\Command\CommandInterface addCommentOnColumn ( string $table, string $column, string $comment )
$table string

The name of the table whose column is to comment.

$column string

The name of the column to comment.

$comment string

The text of the comment.

throws Exception

Note: The method will quote the table, column and comment parameters before using them in the generated SQL.

                public function addCommentOnColumn(string $table, string $column, string $comment): static;

            
addCommentOnTable() public abstract method

Builds an SQL command for adding comment to the table.

public abstract Yiisoft\Db\Command\CommandInterface addCommentOnTable ( string $table, string $comment )
$table string

The name of the table whose column is to comment.

$comment string

The text of the comment to add.

throws Exception

Note: The method will quote the table and comment parameters before using them in the generated SQL.

                public function addCommentOnTable(string $table, string $comment): static;

            
addDefaultValue() public abstract method

Creates an SQL command for adding a default value constraint to an existing table.

public abstract Yiisoft\Db\Command\CommandInterface addDefaultValue ( string $table, string $name, string $column, mixed $value )
$table string

The name of the table to add constraint to.

$name string

The name of the default value constraint.

$column string

The name of the column to add constraint to.

$value mixed

Default value.

throws Yiisoft\Db\Exception\Exception
throws Yiisoft\Db\Exception\NotSupportedException

Note: The method will quote the name, table and column parameters before using them in the generated SQL.

                public function addDefaultValue(string $table, string $name, string $column, mixed $value): static;

            
addForeignKey() public abstract method

Creates an SQL command for adding a foreign key constraint to an existing table.

The method will quote the name, table, referenceTable parameters before using them in the generated SQL.

public abstract Yiisoft\Db\Command\CommandInterface addForeignKey ( string $table, string $name, array|string $columns, string $referenceTable, array|string $referenceColumns, string|null $delete null, string|null $update null )
$table string

The name of the table to add foreign key constraint to.

$name string

The name of the foreign key constraint.

$columns array|string

The name of the column to add foreign key constraint to. If there are many columns, separate them with commas.

$referenceTable string

The name of the table that the foreign key references to.

$referenceColumns array|string

The name of the column that the foreign key references to. If there are many columns, separate them with commas.

$delete string|null

The ON DELETE option. See {@see \Yiisoft\Db\Constant\ReferentialAction} class for possible values.

$update string|null

The ON UPDATE option. See {@see \Yiisoft\Db\Constant\ReferentialAction} class for possible values.

throws Yiisoft\Db\Exception\Exception
throws InvalidArgumentException

                public function addForeignKey(
    string $table,
    string $name,
    array|string $columns,
    string $referenceTable,
    array|string $referenceColumns,
    ?string $delete = null,
    ?string $update = null,
): static;

            
addPrimaryKey() public abstract method

Creates an SQL command for adding a primary key constraint to an existing table.

The method will quote the table and column names.

public abstract Yiisoft\Db\Command\CommandInterface addPrimaryKey ( string $table, string $name, string|string[] $columns )
$table string

The name of the table to add primary key constraint to.

$name string

The name of the primary key constraint.

$columns string|string[]

The comma separated string or array of columns that the primary key consists of.

Note: The method will quote the name, table, and column parameters before using them in the generated SQL.

                public function addPrimaryKey(string $table, string $name, array|string $columns): static;

            
addUnique() public abstract method

Creates an SQL command for adding a unique constraint to an existing table.

public abstract Yiisoft\Db\Command\CommandInterface addUnique ( string $table, string $name, string|string[] $columns )
$table string

The name of the table to add unique constraint to.

$name string

The name of the unique constraint.

$columns string|string[]

The name of the column to add unique constraint to. If there are many columns, use an array or separate them with commas.

Note: The method will quote the name, table, and column parameters before using them in the generated SQL.

                public function addUnique(string $table, string $name, array|string $columns): static;

            
alterColumn() public abstract method

Creates an SQL command for changing the definition of a column.

public abstract Yiisoft\Db\Command\CommandInterface alterColumn ( string $table, string $column, Yiisoft\Db\Schema\Column\ColumnInterface|string $type )
$table string

The table whose column is to change.

$column string

The name of the column to change.

$type Yiisoft\Db\Schema\Column\ColumnInterface|string

The column type. {@see \Yiisoft\Db\Command\QueryBuilder::buildColumnDefinition()} will be called to convert the give column type to the physical one. For example, string will be converted as varchar(255), and string not null becomes varchar(255) not null.

Note: The method will quote the table and column parameters before using them in the generated SQL.

                public function alterColumn(string $table, string $column, ColumnInterface|string $type): static;

            
bindParam() public abstract method

Binds a parameter to the SQL statement to be executed.

public abstract Yiisoft\Db\Command\CommandInterface bindParam ( integer|string $name, mixed &$value, integer|null $dataType null, integer|null $length null, mixed|null $driverOptions null )
$name integer|string

The parameter identifier. For a prepared statement using named placeholders, this will be a parameter name of the form :name. For a prepared statement using question mark placeholders, this will be the 1-indexed position of the parameter.

$value mixed

The PHP variable to bind to the SQL statement parameter (passed by reference).

$dataType integer|null

The {@see \Yiisoft\Db\Constant\DataType SQL data type} of the parameter. If null, the type is determined by the PHP type of the value.

$length integer|null

The length of the data type.

$driverOptions mixed|null

The driver-specific options.

throws Yiisoft\Db\Exception\Exception

                public function bindParam(
    int|string $name,
    mixed &$value,
    ?int $dataType = null,
    ?int $length = null,
    mixed $driverOptions = null,
): static;

            
bindValue() public abstract method

Binds a value to a parameter.

public abstract Yiisoft\Db\Command\CommandInterface bindValue ( integer|string $name, mixed $value, integer|null $dataType null )
$name integer|string

Parameter identifier. For a prepared statement using named placeholders, this will be a parameter name of the form :name. For a prepared statement using question mark placeholders, this will be the 1-indexed position of the parameter.

$value mixed

The value to bind to the parameter.

$dataType integer|null

The {@see \Yiisoft\Db\Constant\DataType SQL data type} of the parameter. If null, the type is determined by the PHP type of the value.

                public function bindValue(int|string $name, mixed $value, ?int $dataType = null): static;

            
bindValues() public abstract method

Binds a list of values to the corresponding parameters.

This is similar to {@see \Yiisoft\Db\Command\bindValue()} except that it binds many values at a time.

Note that the SQL data type of each value is determined by its PHP type.

public abstract Yiisoft\Db\Command\CommandInterface bindValues ( array|Yiisoft\Db\Expression\Value\Param[] $values )
$values array|Yiisoft\Db\Expression\Value\Param[]

The values to bind. This must be given in terms of an associative array with array keys being the parameter names, and an array values the corresponding parameter values, for example, [':name' => 'John Doe', ':age' => 25]. By default, the SQL data type of each value is determined by its PHP type. You may explicitly specify the {@see \Yiisoft\Db\Constant\DataType SQL data type} type by using a {@see \Yiisoft\Db\Expression\Value\Param} class: new Param(value, type), for example, [':name' => 'John Doe', ':profile' => new Param($profile, DataType::LOB)].

                public function bindValues(array $values): static;

            
cancel() public abstract method

Cancels the execution of the SQL statement.

public abstract void cancel ( )

                public function cancel(): void;

            
checkIntegrity() public abstract method

Builds an SQL command for enabling or disabling integrity check.

public abstract Yiisoft\Db\Command\CommandInterface checkIntegrity ( string $schema, string $table, boolean $check true )
$schema string

The schema name of the tables. Defaults to empty string, meaning the current or default schema.

$table string

The table name to check.

$check boolean

Whether to turn the integrity check on or off.

throws Yiisoft\Db\Exception\Exception
throws Yiisoft\Db\Exception\NotSupportedException

Note: The method will quote the table parameters before using them in the generated SQL.

                public function checkIntegrity(string $schema, string $table, bool $check = true): static;

            
createIndex() public abstract method

Creates an SQL command for creating a new index.

public abstract Yiisoft\Db\Command\CommandInterface createIndex ( string $table, string $name, array|string $columns, string|null $indexType null, string|null $indexMethod null )
$table string

The name of the table to create the index for.

$name string

The name of the index.

$columns array|string

The column(s) to include in the index. If there are many columns, separate them with commas.

$indexType string|null

The type of the index supported by DBMS {@see \Yiisoft\Db\Constant\IndexType} - for example: UNIQUE, FULLTEXT, SPATIAL, BITMAP or null as default.

$indexMethod string|null

The index organization method (with USING, not all DBMS).

throws Yiisoft\Db\Exception\Exception
throws InvalidArgumentException

                public function createIndex(
    string $table,
    string $name,
    array|string $columns,
    ?string $indexType = null,
    ?string $indexMethod = null,
): static;

            
createTable() public abstract method

Creates an SQL command 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 is the name of the column which will be properly quoted by the method, and definition is the type of the column which can contain a native database column type, {@see \Yiisoft\Db\Constant\ColumnType abstract} or {@see \Yiisoft\Db\Constant\PseudoType pseudo} type, or can be represented as instance of {@see \Yiisoft\Db\Schema\Column\ColumnInterface}.

The {@see \Yiisoft\Db\Command\QueryBuilderInterface::buildColumnDefinition()} method will be invoked to convert column definitions into SQL representation. For example, it will convert string not null to varchar(255) not null and pk to int PRIMARY KEY AUTO_INCREMENT (for MySQL).

The preferred way is to use {@see \Yiisoft\Db\Command\ColumnBuilder} to generate column definitions as instances of {@see \Yiisoft\Db\Schema\Column\ColumnInterface}.

$this->createTable(
    'example_table',
    [
        'id' => ColumnBuilder::primaryKey(),
        'name' => ColumnBuilder::string(64)->notNull(),
        'type' => ColumnBuilder::integer()->notNull()->defaultValue(10),
        'description' => ColumnBuilder::text(),
        'rule_name' => ColumnBuilder::string(64),
        'data' => ColumnBuilder::text(),
        'created_at' => ColumnBuilder::datetime()->notNull(),
        'updated_at' => ColumnBuilder::datetime(),
    ],
);

If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly put into the generated SQL.

The method will quote the table and columns parameter before using it in the generated SQL.

public abstract Yiisoft\Db\Command\CommandInterface createTable ( string $table, (Yiisoft\Db\Schema\Column\ColumnInterface|Yiisoft\Db\Expression\ExpressionInterface|string)[] $columns, string|null $options null )
$table string

The name of the table to create.

$columns (Yiisoft\Db\Schema\Column\ColumnInterface|Yiisoft\Db\Expression\ExpressionInterface|string)[]

The columns (name => definition) in the new table. The definition can be string, or {@see \Yiisoft\Db\Schema\Column\ColumnInterface} or {@see \Yiisoft\Db\Expression\ExpressionInterface} instance.

$options string|null

More SQL fragments to append to the generated SQL.

throws Yiisoft\Db\Exception\Exception
throws Yiisoft\Db\Exception\InvalidConfigException
throws Yiisoft\Db\Exception\NotSupportedException

                public function createTable(string $table, array $columns, ?string $options = null): static;

            
createView() public abstract method

Creates a SQL View.

public abstract Yiisoft\Db\Command\CommandInterface createView ( string $viewName, Yiisoft\Db\Query\QueryInterface|string $subQuery )
$viewName string

The name of the view to create.

$subQuery Yiisoft\Db\Query\QueryInterface|string

The select statement which defines the view. This can be either a string or a {@see \Yiisoft\Db\Query\QueryInterface}.

throws InvalidArgumentException
throws Yiisoft\Db\Exception\Exception

Note: The method will quote the viewName parameter before using it in the generated SQL.

                public function createView(string $viewName, QueryInterface|string $subQuery): static;

            
delete() public abstract method

Creates a DELETE command.

For example,

$connectionInterface->createCommand()->delete('user', 'status = 0')->execute();

or with using parameter binding for the condition:

$status = 0;
$connectionInterface->createCommand()->delete('user', 'status = :status', [':status' => $status])->execute();

The method will escape the table and column names.

Note that the created command isn't executed until you call {@see \Yiisoft\Db\Command\execute()}.

public abstract Yiisoft\Db\Command\CommandInterface delete ( string $table, array|string $condition '', array $params = [] )
$table string

The table to delete data from.

$condition array|string

The condition to put in the WHERE part. Please refer to {@see \Yiisoft\Db\Query\QueryInterface::where()} on how to specify condition.

$params array

The parameters to bind to the command.

throws Yiisoft\Db\Exception\Exception
throws InvalidArgumentException

                public function delete(string $table, array|string $condition = '', array $params = []): static;

            
dropCheck() public abstract method

Creates an SQL command for dropping a check constraint.

public abstract Yiisoft\Db\Command\CommandInterface dropCheck ( string $table, string $name )
$table string

The name of the table whose check constraint to drop.

$name string

The name of the check constraint to drop.

Note: The method will quote the name and table parameters before using them in the generated SQL.

                public function dropCheck(string $table, string $name): static;

            
dropColumn() public abstract method

Creates an SQL command for dropping a DB column.

public abstract Yiisoft\Db\Command\CommandInterface dropColumn ( string $table, string $column )
$table string

The name of the table whose column is to drop.

$column string

The name of the column to drop.

Note: The method will quote the table and column parameters before using them in the generated SQL.

                public function dropColumn(string $table, string $column): static;

            
dropCommentFromColumn() public abstract method

Builds an SQL command for dropping comment from column.

public abstract Yiisoft\Db\Command\CommandInterface dropCommentFromColumn ( string $table, string $column )
$table string

The name of the table whose column to comment.

$column string

The name of the column to comment.

Note: The method will quote the table and column parameters before using them in the generated SQL.

                public function dropCommentFromColumn(string $table, string $column): static;

            
dropCommentFromTable() public abstract method

Builds an SQL command for dropping comment from the table.

public abstract Yiisoft\Db\Command\CommandInterface dropCommentFromTable ( string $table )
$table string

The name of the table whose column to comment.

Note: The method will quote the table parameter before using it in the generated SQL.

                public function dropCommentFromTable(string $table): static;

            
dropDefaultValue() public abstract method

Creates an SQL command for dropping a default value constraint.

public abstract Yiisoft\Db\Command\CommandInterface dropDefaultValue ( string $table, string $name )
$table string

The name of the table whose default value constraint to drop.

$name string

The name of the default value constraint to drop.

throws Yiisoft\Db\Exception\Exception
throws Yiisoft\Db\Exception\NotSupportedException

Note: The method will quote the name and table parameters before using them in the generated SQL.

                public function dropDefaultValue(string $table, string $name): static;

            
dropForeignKey() public abstract method

Creates an SQL command for dropping a foreign key constraint.

public abstract Yiisoft\Db\Command\CommandInterface dropForeignKey ( string $table, string $name )
$table string

The name of the table whose foreign is to drop.

$name string

The name of the foreign key constraint to drop.

Note: The method will quote the name and table parameters before using them in the generated SQL.

                public function dropForeignKey(string $table, string $name): static;

            
dropIndex() public abstract method

Creates an SQL command for dropping an index.

public abstract Yiisoft\Db\Command\CommandInterface dropIndex ( string $table, string $name )
$table string

The name of the table whose index to drop.

$name string

The name of the index to drop.

Note: The method will quote the name and table parameters before using them in the generated SQL.

                public function dropIndex(string $table, string $name): static;

            
dropPrimaryKey() public abstract method

Creates an SQL command for removing a primary key constraint to an existing table.

public abstract Yiisoft\Db\Command\CommandInterface dropPrimaryKey ( string $table, string $name )
$table string

The name of the table to remove the primary key constraint from.

$name string

The name of the primary key constraint to remove.

Note: The method will quote the name and table parameters before using them in the generated SQL.

                public function dropPrimaryKey(string $table, string $name): static;

            
dropTable() public abstract method

Creates an SQL command for dropping a DB table.

public abstract Yiisoft\Db\Command\CommandInterface dropTable ( string $table, boolean $ifExists false, boolean $cascade false )
$table string

The name of the table to drop.

$ifExists boolean

Do not throw an error if the table does not exist.

$cascade boolean

Automatically drop objects that depend on the table.

Note: The method will quote the table parameter before using it in the generated SQL.

                public function dropTable(string $table, bool $ifExists = false, bool $cascade = false): static;

            
dropUnique() public abstract method

Creates an SQL command for dropping a unique constraint.

public abstract Yiisoft\Db\Command\CommandInterface dropUnique ( string $table, string $name )
$table string

The name of the table whose unique constraint to drop.

$name string

The name of the unique constraint to drop.

Note: The method will quote the name and table parameters before using them in the generated SQL.

                public function dropUnique(string $table, string $name): static;

            
dropView() public abstract method

Drops an SQL View.

public abstract Yiisoft\Db\Command\CommandInterface dropView ( string $viewName )
$viewName string

The name of the view to drop.

Note: The method will quote the viewName parameter before using it in the generated SQL.

                public function dropView(string $viewName): static;

            
execute() public abstract method

Executes the SQL statement.

You should use this method only for executing a non-query SQL statement, such as INSERT, DELETE, UPDATE SQLs. It returns no result set.

public abstract integer execute ( )
return integer

The number of rows execution affected.

throws Yiisoft\Db\Exception\Exception
throws Throwable

If execution failed.

                public function execute(): int;

            
getParams() public abstract method

Return the params used in the last query.

public abstract array getParams ( boolean $asValues true )
$asValues boolean

By default, returns an array of name => value pairs. If set to true, returns an array of {@see \Yiisoft\Db\Expression\Value\Param}.

return array

The params used in the last query.

                public function getParams(bool $asValues = true): array;

            
getRawSql() public abstract method

Returns the raw SQL by inserting parameter values into the corresponding placeholders in {@see getSql()}.

Note that you should mainly use the return value of this method for logging.

It's likely that this method returns an invalid SQL due to improper replacement of parameter placeholders.

public abstract string getRawSql ( )
return string

The raw SQL with parameter values inserted into the corresponding placeholders in {@see \Yiisoft\Db\Command\getSql()}.

throws Exception

                public function getRawSql(): string;

            
getSql() public abstract method

Returns the SQL statement for this command.

public abstract string getSql ( )
return string

The SQL statement to execute.

                public function getSql(): string;

            
insert() public abstract method

Creates an INSERT command.

For example,

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

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

Note that the created command isn't executed until you call {@see \Yiisoft\Db\Command\execute()}.

public abstract Yiisoft\Db\Command\CommandInterface insert ( string $table, array|Yiisoft\Db\Query\QueryInterface $columns )
$table string

The name of the table to insert new rows into.

$columns array|Yiisoft\Db\Query\QueryInterface

The column data (name => value) to insert into the table or an instance of {@see \Yiisoft\Db\Query\QueryInterface} to perform INSERT INTO ... SELECT SQL statement.

throws Yiisoft\Db\Exception\Exception
throws InvalidArgumentException
throws Yiisoft\Db\Exception\InvalidConfigException
throws Yiisoft\Db\Exception\NotSupportedException

Note: The method will quote the table and columns parameter before using it in the generated SQL.

                public function insert(string $table, array|QueryInterface $columns): static;

            
insertBatch() public abstract method

Creates a batch INSERT command.

For example,

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

or as associative arrays where the keys are column names

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

The method will escape the column names, and quote the values to insert.

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

Also note that the created command isn't executed until {@see \Yiisoft\Db\Command\execute()} is called.

public abstract Yiisoft\Db\Command\CommandInterface insertBatch ( string $table, iterable $rows, string[] $columns = [] )
$table string

The name of the table to insert new rows into.

$rows iterable

The rows to be batch inserted into the table.

$columns string[]

The column names.

throws Yiisoft\Db\Exception\Exception
throws InvalidArgumentException

                public function insertBatch(string $table, iterable $rows, array $columns = []): static;

            
insertReturningPks() public abstract method

Attention! Please use function only as a last resort. The feature will be refactored in future releases.

Executes the INSERT command, returning primary key inserted values.

public abstract array insertReturningPks ( string $table, array|Yiisoft\Db\Query\QueryInterface $columns )
$table string

The name of the table to insert new rows into.

$columns array|Yiisoft\Db\Query\QueryInterface

The column data (name => value) to insert into the table or an instance of {@see \Yiisoft\Db\Query\QueryInterface} to perform INSERT INTO ... SELECT SQL statement.

return array

The primary key values.

Note: The method will quote the table and columns parameter before using it in the generated SQL.

throws Yiisoft\Db\Exception\Exception
throws Yiisoft\Db\Exception\InvalidCallException
throws Yiisoft\Db\Exception\InvalidConfigException
throws Throwable

                public function insertReturningPks(string $table, array|QueryInterface $columns): array;

            
prepare() public abstract method

Prepares the SQL statement to be executed.

For complex SQL statement that's to be executed many times, this may improve performance.

For SQL statement with binding parameters, this method is invoked automatically.

public abstract void prepare ( boolean|null $forRead null )
$forRead boolean|null

Whether the method call is for a read query. If null, it means the SQL statement should be used to decide whether it's to read or write.

throws Yiisoft\Db\Exception\Exception

If there is any DB error.

throws Yiisoft\Db\Exception\InvalidConfigException

                public function prepare(?bool $forRead = null): void;

            
query() public abstract method

Executes the SQL statement and returns a query result.

This method is for executing an SQL query that returns result set, such as SELECT.

public abstract Yiisoft\Db\Query\DataReaderInterface query ( )
return Yiisoft\Db\Query\DataReaderInterface

The reader object for fetching the query result.

throws Yiisoft\Db\Exception\Exception
throws Throwable

If execution failed.

                public function query(): DataReaderInterface;

            
queryAll() public abstract method

Executes the SQL statement and returns ALL rows at once.

public abstract array[] queryAll ( )
return array[]

All rows of the query result. Each array element is an array representing a row of data. Empty array if the query results in nothing.

throws Yiisoft\Db\Exception\Exception
throws Throwable

If execution failed.

                public function queryAll(): array;

            
queryColumn() public abstract method

Execute the SQL statement and returns the first column of the result.

This method is best used when you need only the first column of a result (that's the first element in each row).

public abstract array queryColumn ( )
return array

The first column of the query result. Empty array if the query results in nothing.

throws Yiisoft\Db\Exception\Exception
throws Throwable

If execution failed.

                public function queryColumn(): array;

            
queryOne() public abstract method

Executes the SQL statement and returns the first row of the result.

This method is best used when you need only the first row of a result.

public abstract array|null queryOne ( )
return array|null

The first row as an array of the query result. null if the query results in nothing.

throws Yiisoft\Db\Exception\Exception
throws Throwable

If execution failed.

                public function queryOne(): ?array;

            
queryScalar() public abstract method

Execute the SQL statement and returns the value of the first column in the first row of data.

This method is best used when you need only a single value. Do not use this method for boolean values as it returns false if there is no value.

public abstract false|float|integer|string|null queryScalar ( )
return false|float|integer|string|null

The value of the first column in the first row of the query result. False if there is no value.

throws Yiisoft\Db\Exception\Exception
throws Throwable

If execution failed.

                public function queryScalar(): bool|string|int|float|null;

            
renameColumn() public abstract method

Creates an SQL command for renaming a column.

public abstract Yiisoft\Db\Command\CommandInterface renameColumn ( string $table, string $oldName, string $newName )
$table string

The name of the table whose column is to rename.

$oldName string

The old name of the column.

$newName string

The new name of the column.

Note: The method will quote the table, oldName and newName parameter before using it in the generated SQL.

                public function renameColumn(string $table, string $oldName, string $newName): static;

            
renameTable() public abstract method

Creates an SQL command for renaming a DB table.

public abstract Yiisoft\Db\Command\CommandInterface renameTable ( string $table, string $newName )
$table string

The name of the table to rename.

$newName string

The new table name.

Note: The method will quote the table and newName parameter before using it in the generated SQL.

                public function renameTable(string $table, string $newName): static;

            
resetSequence() public abstract method

Executes a db command resetting the sequence value of a table's primary key.

Reason for execute is that some databases (Oracle) need several queries to do so.

The sequence is reset such that the primary key of the next new row inserted will have the specified value or the maximum existing value +1.

public abstract Yiisoft\Db\Command\CommandInterface resetSequence ( string $table, integer|string|null $value null )
$table string

The name of the table whose primary key sequence is reset.

$value integer|string|null

The value for the primary key of the next new row inserted. If this isn't set, the next new row's primary key will have the maximum existing value +1.

throws Yiisoft\Db\Exception\Exception
throws Yiisoft\Db\Exception\NotSupportedException

Note: The method will quote the table parameter before using it in the generated SQL.

                public function resetSequence(string $table, int|string|null $value = null): static;

            
setRawSql() public abstract method

Specifies the SQL statement to execute.

The SQL statement won't be modified in any way.

The earlier SQL (if any) will be discarded, and {@see \Yiisoft\Db\Expression\Value\Param} will be cleared as well.

See {@see \Yiisoft\Db\Command\reset()} for details.

See also:

public abstract Yiisoft\Db\Command\CommandInterface setRawSql ( string $sql )
$sql string

The SQL statement to set.

                public function setRawSql(string $sql): static;

            
setRetryHandler() public abstract method

Sets a closure (anonymous function) which called when a database exception is thrown when executing the command.

The signature of the closure should be:

use Yiisoft\Db\Exception\Exception;

function (Exception $e, int $attempt): bool
{
    // return true or false (whether to retry the command or throw $e)
}

The closure will receive an {@see \Yiisoft\Db\Exception\Exception} converted from the thrown database exception and the current attempt to execute the command, starting from 1.

If the closure returns true, the command will be retried. If the closure returns false, the {@see \Yiisoft\Db\Exception\Exception} will be thrown.

public abstract Yiisoft\Db\Command\CommandInterface setRetryHandler ( Closure|null $handler )
$handler Closure|null

A PHP callback to handle database exceptions.

                public function setRetryHandler(?Closure $handler): static;

            
setSql() public abstract method

Specifies the SQL statement to execute. The SQL statement will be quoted using {@see ConnectionInterface::quoteSql()}.

The previous SQL (if any) will be discarded, and {@see \Yiisoft\Db\Expression\Value\Param} will be cleared as well. See {@see \Yiisoft\Db\Command\reset()} for details.

See also:

public abstract Yiisoft\Db\Command\CommandInterface setSql ( string $sql )
$sql string

The SQL statement to set.

                public function setSql(string $sql): static;

            
showDatabases() public abstract method

List all database names in the current connection.

public abstract array showDatabases ( )

                public function showDatabases(): array;

            
truncateTable() public abstract method

Creates an SQL command for truncating a DB table.

public abstract Yiisoft\Db\Command\CommandInterface truncateTable ( string $table )
$table string

The table to truncate.

Note: The method will quote the table parameter before using it in the generated SQL.

                public function truncateTable(string $table): static;

            
update() public abstract method

Creates an UPDATE command.

For example,

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

or with using parameter binding for the condition:

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

The method will escape the column names and bind the values to update.

Note that the created command isn't executed until you call {@see \Yiisoft\Db\Command\execute()}.

public abstract Yiisoft\Db\Command\CommandInterface update ( string $table, array $columns, array|Yiisoft\Db\Expression\ExpressionInterface|string $condition '', array|Yiisoft\Db\Expression\ExpressionInterface|string|null $from null, array $params = [] )
$table string

The name of the table to update.

$columns array

The column data (name => value) to update.

$condition array|Yiisoft\Db\Expression\ExpressionInterface|string

The condition to put in the WHERE part. Please refer to {@see \Yiisoft\Db\Command\QueryPartsInterface::where()} on how to specify condition.

$from array|Yiisoft\Db\Expression\ExpressionInterface|string|null

The FROM part. Please refer to {@see \Yiisoft\Db\Command\QueryPartsInterface::from()} on how to specify FROM part.

$params array

The parameters to bind to the command.

throws Yiisoft\Db\Exception\Exception
throws InvalidArgumentException

Note: The method will quote the table and columns parameter before using it in the generated SQL.

                public function update(
    string $table,
    array $columns,
    array|ExpressionInterface|string $condition = '',
    array|ExpressionInterface|string|null $from = null,
    array $params = [],
): static;

            
upsert() public abstract method

Creates a command to insert rows into a database table if they don't 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 \Yiisoft\Db\Expression\Expression('visits + 1'),
    ],
    $params,
);

The method will escape the table and column names.

public abstract Yiisoft\Db\Command\CommandInterface upsert ( string $table, array|Yiisoft\Db\Query\QueryInterface $insertColumns, array|boolean $updateColumns true )
$table string

The name of the table to insert rows into or update rows in.

$insertColumns array|Yiisoft\Db\Query\QueryInterface

The column data (name => value) to insert into the table or an instance of {@see \Yiisoft\Db\Query\QueryInterface} to perform INSERT INTO ... SELECT SQL statement.

$updateColumns array|boolean

The column data (name => value) to update if it already exists. 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 exist.

throws Yiisoft\Db\Exception\Exception
throws Yiisoft\Db\Exception\InvalidConfigException
throws JsonException
throws Yiisoft\Db\Exception\NotSupportedException

                public function upsert(
    string $table,
    array|QueryInterface $insertColumns,
    array|bool $updateColumns = true,
): static;

            
upsertReturning() public abstract method

Creates a command to insert rows into a database table if they don't already exist (matching unique constraints) or update them if they do, returning values from the specified columns.

The method will quote the table, insertColumns, updateColumns and returnColumns parameters before using it in the generated SQL.

See also upsertReturningPks().

public abstract array upsertReturning ( string $table, array|Yiisoft\Db\Query\QueryInterface $insertColumns, array|boolean $updateColumns true, string[]|null $returnColumns null )
$table string

The name of the table to insert rows into or update rows in.

$insertColumns array|Yiisoft\Db\Query\QueryInterface

The column data (name => value) to insert into the table or an instance of {@see \Yiisoft\Db\Query\QueryInterface} to perform INSERT INTO ... SELECT SQL statement.

$updateColumns array|boolean

The column data (name => value) to update if it already exists. 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 exist.

$returnColumns string[]|null

The column names to return values from. null means all columns.

throws Yiisoft\Db\Exception\Exception
throws Yiisoft\Db\Exception\InvalidConfigException
throws JsonException
throws Yiisoft\Db\Exception\NotSupportedException

                public function upsertReturning(
    string $table,
    array|QueryInterface $insertColumns,
    array|bool $updateColumns = true,
    ?array $returnColumns = null,
): array;

            
upsertReturningPks() public abstract method

Creates a command to insert rows into a database table if they don't already exist (matching unique constraints) or update them if they do, with returning inserted or updated primary key values.

The method will quote the table, insertColumns and updateColumns parameters before using it in the generated SQL.

See also upsertReturning().

public abstract array upsertReturningPks ( string $table, array|Yiisoft\Db\Query\QueryInterface $insertColumns, array|boolean $updateColumns true )
$table string

The name of the table to insert rows into or update rows in.

$insertColumns array|Yiisoft\Db\Query\QueryInterface

The column data (name => value) to insert into the table or an instance of {@see \Yiisoft\Db\Query\QueryInterface} to perform INSERT INTO ... SELECT SQL statement.

$updateColumns array|boolean

The column data (name => value) to update if it already exists. 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 exist.

throws Yiisoft\Db\Exception\Exception
throws Yiisoft\Db\Exception\InvalidConfigException
throws JsonException
throws Yiisoft\Db\Exception\NotSupportedException

                public function upsertReturningPks(
    string $table,
    array|QueryInterface $insertColumns,
    array|bool $updateColumns = true,
): array;

            
withDbTypecasting() public abstract method

Returns copy of the instance with enabled or disabled typecasting of values when inserting or updating records.

public abstract Yiisoft\Db\Command\CommandInterface withDbTypecasting ( boolean $dbTypecasting true )
$dbTypecasting boolean

                public function withDbTypecasting(bool $dbTypecasting = true): static;

            
withPhpTypecasting() public abstract method

Returns a copy of the instance with enabled or disabled typecasting of values when retrieving records from DB.

public abstract Yiisoft\Db\Command\CommandInterface withPhpTypecasting ( boolean $phpTypecasting true )
$phpTypecasting boolean

                public function withPhpTypecasting(bool $phpTypecasting = true): static;

            
withTypecasting() public abstract method

Returns a copy of the instance with enabled or disabled typecasting of values when inserting, updating or retrieving records from DB.

public abstract Yiisoft\Db\Command\CommandInterface withTypecasting ( boolean $typecasting true )
$typecasting boolean

                public function withTypecasting(bool $typecasting = true): static;