# Class yii\sphinx\Command

Inheritance | yii\sphinx\Command » yii\db\Command » yii\base\Component » yii\base\BaseObject
Implements | yii\base\Configurable
Available since version | 2.0
Source Code | https://github.com/yiisoft/yii2-sphinx/blob/master/Command.php

Command represents a SQL statement to be executed against a Sphinx.

A command object is usually created by calling yii\sphinx\Connection::createCommand(). The SQL statement it represents can be set via the $sql property.

To execute a non-query SQL (such as INSERT, REPLACE, DELETE, UPDATE), call execute(). To execute a SQL statement that returns result data set (such as SELECT, CALL SNIPPETS, CALL KEYWORDS), use queryAll(), queryOne(), queryColumn(), queryScalar(), or query(). For example,

    $articles = $connection->createCommand("SELECT * FROM `idx_article` WHERE MATCH('programming')")->queryAll();

Command supports SQL statement preparation and parameter binding just as yii\db\Command does.

Command also supports building SQL statements by providing methods such as insert(), update(), etc. For example,

    $connection->createCommand()->update('idx_article', [
        'genre_id' => 15,
        'author_id' => 157,
    ])->execute();

To build SELECT SQL statements, please use yii\sphinx\Query and yii\sphinx\QueryBuilder instead.

## Public Properties

Hide inherited properties

Property | Type | Description | Defined By
--- | --- | --- | ---
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component. This property is read-only. | yii\base\Component
$db | yii\sphinx\Connection | The Sphinx connection that this command is associated with. | yii\sphinx\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
$rawSql | string | The raw SQL with parameter values inserted into the corresponding placeholders in $sql. | yii\db\Command
$sql | string | The SQL statement to be executed. | yii\db\Command

## Protected Properties

Hide inherited properties

Property | Type | Description | Defined By
--- | --- | --- | ---
$pendingParams | array | Pending parameters to be bound to the current PDO statement. | yii\db\Command

## Public Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
__call() | Calls the named method which is not a class method. | yii\base\Component
__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 a component property. | yii\base\Component
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Component
__set() | Sets the value of a component property. | yii\base\Component
__unset() | Sets a component property to be null. | yii\base\Component
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\sphinx\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\sphinx\Command
addPrimaryKey() | Creates a SQL command for adding a primary key constraint to an existing table. | yii\sphinx\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\sphinx\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. | yii\sphinx\Command
batchReplace() | Creates a batch REPLACE command. | yii\sphinx\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\sphinx\Command
bindValues() | Binds a list of values to the corresponding parameters. | yii\sphinx\Command
cache() | Enables query cache for this command. | yii\db\Command
callKeywords() | Returns tokenized and normalized forms of the keywords, and, optionally, keyword statistics. | yii\sphinx\Command
callSnippets() | Builds a snippet from provided data and query, using specified index settings. | yii\sphinx\Command
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\Component
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Component
cancel() | Cancels the execution of the SQL statement. | yii\db\Command
checkIntegrity() | Builds a SQL command for enabling or disabling integrity check. | yii\sphinx\Command
className() | Returns the fully qualified name of this class. | yii\base\BaseObject
createIndex() | Creates a SQL command for creating a new index. | yii\sphinx\Command
createTable() | Creates a SQL command for creating a new DB table. | yii\sphinx\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\sphinx\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\sphinx\Command
dropIndex() | Creates a SQL command for dropping an index. | yii\sphinx\Command
dropPrimaryKey() | Creates a SQL command for removing a primary key constraint to an existing table. | yii\sphinx\Command
dropTable() | Creates a SQL command for dropping a DB table. | yii\sphinx\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
executeResetSequence() | Executes a db command resetting the sequence value of a table's primary key. | 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 $sql. | yii\sphinx\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\Component
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\base\Component
init() | Initializes the object. | yii\base\BaseObject
insert() | Creates an INSERT command. | yii\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\sphinx\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\sphinx\Command
renameTable() | Creates a SQL command for renaming a DB table. | yii\sphinx\Command
replace() | Creates an REPLACE command. | yii\sphinx\Command
resetSequence() | Creates a SQL command for resetting the sequence value of a table's primary key. | yii\sphinx\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
truncateIndex() | Creates a SQL command for truncating a runtime index. | yii\sphinx\Command
truncateTable() | Creates a SQL command for truncating a DB table. | yii\sphinx\Command
update() | Creates an UPDATE command. | yii\sphinx\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. | yii\db\Command

## Protected Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
bindPendingParams() | Binds pending parameters that were registered via bindValue() and bindValues(). | yii\db\Command
getCacheKey() | Returns the cache key for the query. | yii\db\Command
internalExecute() | Executes a prepared statement. | yii\db\Command
logQuery() | Logs the current database query if query logging is enabled and returns the profiling token if profiling is enabled. | 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

## Property Details

$db public property

The Sphinx connection that this command is associated with.

public yii\sphinx\Connection $db = null

## Method Details

addColumn() public method

Creates a SQL command for adding a new DB column.

public $this 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. yii\db\QueryBuilder::getColumnType() 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`.
return | $this | The command object itself

addForeignKey() public method

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

The method will properly quote the table and column names.

public $this 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.
$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.
$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 | $this | The command object itself

addPrimaryKey() public method

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

The method will properly quote the table and column names.

public $this 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 | $this | The command object itself.

alterColumn() public method

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

public $this 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 column type. yii\db\QueryBuilder::getColumnType() 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`.
return | $this | The command object itself

batchInsert() public method

Creates a batch INSERT command.

For example,

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

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

public $this batchInsert ( $index, $columns, $rows )
$index | string | The index that new rows will be inserted into.
$columns | array | The column names
$rows | array | The rows to be batch inserted into the index
return | $this | The command object itself

batchReplace() public method

Creates a batch REPLACE command.

For example,

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

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

public $this batchReplace ( $index, $columns, $rows )
$index | string | The index that new rows will be replaced.
$columns | array | The column names
$rows | array | The rows to be batch replaced in the index
return | $this | The command object itself

bindValue() public method

Binds a value to a parameter.

public $this bindValue ( $name, $value, $dataType = null )
$name | string|integer | 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 | SQL data type of the parameter. If null, the type is determined by the PHP type of the value.
return | $this | The current command being executed

bindValues() public method

Binds a list of values to the corresponding parameters.

This is similar to bindValue() except that it binds multiple values at a time. Note that the SQL data type of each value is determined by its PHP type.

public $this bindValues ( $values )
$values | array | The values to be bound. This must be given in terms of an associative array with array keys being the parameter names, and array values the corresponding parameter values, e.g. `[':name' => 'John', ':age' => 25]`. By default, the PDO type of each value is determined by its PHP type. You may explicitly specify the PDO type by using a yii\db\PdoValue class: `new PdoValue(value, type)`, e.g. `[':name' => 'John', ':profile' => new PdoValue($profile, \PDO::PARAM_LOB)]`.
return | $this | The current command being executed

callKeywords() public method

Returns tokenized and normalized forms of the keywords, and, optionally, keyword statistics.

public $this callKeywords ( $index, $text, $fetchStatistic = false )
$index | string | The name of the index from which to take the text processing settings
$text | string | The text to break down to keywords.
$fetchStatistic | boolean | Whether to return document and hit occurrence statistics
return | $this | The command object itself

callSnippets() public method

Builds a snippet from provided data and query, using specified index settings.

public $this callSnippets ( $index, $source, $match, $options = [] )
$index | string | Name of the index, from which to take the text processing settings.
$source | string|array | Is the source data to extract a snippet from. It could be either a single string or array of strings.
$match | string | The full-text query to build snippets for.
$options | array | List of options in format: optionName => optionValue
return | $this | The command object itself

checkIntegrity() public method

Builds a SQL command for enabling or disabling integrity check.

public $this checkIntegrity ( $check = true, $schema = '', $table = '' )
$check | boolean | Whether to turn on or off the integrity check.
$schema | string | The schema name of the tables. Defaults to empty string, meaning the current or default schema.
$table | string | The table name.
return | $this | The command object itself
throws | yii\base\NotSupportedException | if this is not supported by the underlying DBMS

createIndex() public method

Creates a SQL command for creating a new index.

public $this 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, please separate them by commas. The column names will be properly quoted by the method.
$unique | boolean | Whether to add UNIQUE constraint on the created index.
return | $this | The command object itself

createTable() public method

Creates a 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 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 method yii\db\QueryBuilder::getColumnType() will be called to convert the abstract column types to physical ones. For example, `string` will be converted as `varchar(255)`, and `string not null` becomes `varchar(255) not null`.

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

public $this 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 | $this | The command object itself

dropColumn() public method

Creates a SQL command for dropping a DB column.

public $this 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 | $this | The command object itself

dropForeignKey() public method

Creates a SQL command for dropping a foreign key constraint.

public $this 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 | $this | The command object itself

dropIndex() public method

Creates a SQL command for dropping an index.

public $this 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 | $this | The command object itself

dropPrimaryKey() public method

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

public $this 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 | $this | The command object itself

dropTable() public method

Creates a SQL command for dropping a DB table.

public $this dropTable ( $table )
$table | string | The table to be dropped. The name will be properly quoted by the method.
return | $this | The command object itself

getRawSql() public method

Returns the raw SQL by inserting parameter values into the corresponding placeholders in $sql.

Note that the return value of this method should mainly be used for logging purpose. It is likely that this method returns an invalid SQL due to improper replacement of parameter placeholders.

public string getRawSql ( )
return | string | The raw SQL with parameter values inserted into the corresponding placeholders in $sql.

prepare() public method

Prepares the SQL statement to be executed.

For complex SQL statement that is to be executed multiple times, this may improve performance. For SQL statement with binding parameters, this method is invoked automatically.

public void prepare ( $forRead = null )
$forRead | boolean | Whether this method is called for a read query. If null, it means the SQL statement should be used to determine whether it is for read or write.
throws | yii\db\Exception | if there is any DB error

renameColumn() public method

Creates a SQL command for renaming a column.

public $this 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 | $this | The command object itself

renameTable() public method

Creates a SQL command for renaming a DB table.

public $this renameTable ( $table, $newName )
$table | 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 | $this | The command object itself

replace() public method

Creates an REPLACE command.

For example,

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

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

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

public $this replace ( $index, $columns )
$index | string | The index that new rows will be replaced into.
$columns | array | The column data (name => value) to be replaced into the index.
return | $this | The command object itself

resetSequence() public method

Creates a SQL command 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 the maximum existing value +1.

public $this resetSequence ( $table, $value = null )
$table | 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 the maximum existing value +1.
return | $this | The command object itself
throws | yii\base\NotSupportedException | if this is not supported by the underlying DBMS

truncateIndex() public method

Creates a SQL command for truncating a runtime index.

public $this truncateIndex ( $index )
$index | string | The index to be truncated. The name will be properly quoted by the method.
return | $this | The command object itself

truncateTable() public method

Creates a SQL command for truncating a DB table.

public $this truncateTable ( $table )
$table | string | The table to be truncated. The name will be properly quoted by the method.
return | $this | The command object itself

update() public method

Creates an UPDATE command.

For example,

    $connection->createCommand()->update('user', ['status' => 1], 'age > 30')->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 $this update ( $index, $columns, $condition = '', $params = [], $options = [] )
$index | string | The index 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 yii\sphinx\Query::where() on how to specify condition.
$params | array | The parameters to be bound to the command
$options | array | List of options in format: optionName => optionValue
return | $this | The command object itself