A command object is usually created by calling [[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 Command does. Command also supports building SQL statements by providing methods such as [[insert()]], Command::update, etc. For example, ~~~ $connection->createCommand()->update('idx_article', [ 'genre_id' => 15, 'author_id' => 157, ])->execute(); ~~~ To build SELECT SQL statements, please use Query and QueryBuilder instead.
Since: 2.0
Author: Paul Klimov (klimov.paul@gmail.com)
Inheritance: extends yii\db\Command
Exemple #1
0
 /**
  * Creates a command for execution.
  * @param string $sql the SQL statement to be executed
  * @param array $params the parameters to be bound to the SQL statement
  * @return Command the Sphinx command
  */
 public function createCommand($sql = null, $params = [])
 {
     $command = new Command(['db' => $this, 'sql' => $sql]);
     return $command->bindValues($params);
 }