diff --git a/framework/db/Command.php b/framework/db/Command.php index 0fb099a..3531fa7 100644 --- a/framework/db/Command.php +++ b/framework/db/Command.php @@ -485,16 +485,41 @@ class Command extends \yii\base\Component * * @param string $table the table that new rows will be inserted into. * @param array $columns the column data (name=>value) to be inserted into the table. - * @param array $params the parameters to be bound to the command * @return Command the command object itself */ - public function insert($table, $columns, $params = array()) + public function insert($table, $columns) { + $params = array(); $sql = $this->db->getQueryBuilder()->insert($table, $columns, $params); return $this->setSql($sql)->bindValues($params); } /** + * Creates a batch INSERT command. + * For example, + * + * ~~~ + * $connection->createCommand()->batchInsert('tbl_user', array('name', 'age'), array( + * array('Tom', 30), + * array('Jane', 20), + * array('Linda', 25), + * ))->execute(); + * ~~~ + * + * Not that the values in each row must match the corresponding column names. + * + * @param string $table the table that new rows will be inserted into. + * @param array $columns the column names + * @param array $rows the rows to be batch inserted into the table + * @return Command the command object itself + */ + public function batchInsert($table, $columns, $rows) + { + $sql = $this->db->getQueryBuilder()->batchInsert($table, $columns, $rows); + return $this->setSql($sql); + } + + /** * Creates an UPDATE command. * For example, * diff --git a/framework/db/QueryBuilder.php b/framework/db/QueryBuilder.php index d40da91..35bfcb3 100644 --- a/framework/db/QueryBuilder.php +++ b/framework/db/QueryBuilder.php @@ -115,6 +115,33 @@ class QueryBuilder extends \yii\base\Object } /** + * Generates a batch INSERT SQL statement. + * For example, + * + * ~~~ + * $connection->createCommand()->batchInsert('tbl_user', array('name', 'age'), array( + * array('Tom', 30), + * array('Jane', 20), + * array('Linda', 25), + * ))->execute(); + * ~~~ + * + * Not that the values in each row must match the corresponding column names. + * + * @param string $table the table that new rows will be inserted into. + * @param array $columns the column names + * @param array $rows the rows to be batch inserted into the table + * @param array $params the parameters to be bound to the command + * @return string the batch INSERT SQL statement + * @throws NotSupportedException if this is not supported by the underlying DBMS + */ + public function batchInsert($table, $columns, $rows, $params = array()) + { + throw new NotSupportedException($this->db->getDriverName() . ' does not support batch insert.'); + + } + + /** * Creates an UPDATE SQL statement. * For example, * diff --git a/framework/db/mysql/QueryBuilder.php b/framework/db/mysql/QueryBuilder.php index 73986af..6168409 100644 --- a/framework/db/mysql/QueryBuilder.php +++ b/framework/db/mysql/QueryBuilder.php @@ -129,4 +129,39 @@ class QueryBuilder extends \yii\db\QueryBuilder { return 'SET FOREIGN_KEY_CHECKS=' . ($check ? 1 : 0); } + + /** + * Generates a batch INSERT SQL statement. + * For example, + * + * ~~~ + * $connection->createCommand()->batchInsert('tbl_user', array('name', 'age'), array( + * array('Tom', 30), + * array('Jane', 20), + * array('Linda', 25), + * ))->execute(); + * ~~~ + * + * Not that the values in each row must match the corresponding column names. + * + * @param string $table the table that new rows will be inserted into. + * @param array $columns the column names + * @param array $rows the rows to be batch inserted into the table + * @return string the batch INSERT SQL statement + */ + public function batchInsert($table, $columns, $rows) + { + $values = array(); + foreach ($rows as $row) { + $vs = array(); + foreach ($row as $value) { + $vs[] = is_string($value) ? $this->db->quoteValue($value) : $value; + } + $values[] = $vs; + } + + return 'INSERT INTO ' . $this->db->quoteTableName($table) + . ' (' . implode(', ', $columns) . ') VALUES (' + . implode(', ', $values) . ')'; + } }