一次插入多行我已經通過http://www.yiiframework.com/doc/guide/1.1/en/form.table在誼
走後,我不明白這是什麼意思:
$items=$this->getItemsToUpdate();
這是什麼$this->getItemsToUpdate()
功能?
我試圖一次插入動態行。我用jquery創建數據,但我不知道如何將它們插入到數據庫中。
一次插入多行我已經通過http://www.yiiframework.com/doc/guide/1.1/en/form.table在誼
走後,我不明白這是什麼意思:
$items=$this->getItemsToUpdate();
這是什麼$this->getItemsToUpdate()
功能?
我試圖一次插入動態行。我用jquery創建數據,但我不知道如何將它們插入到數據庫中。
如果您不受限於使用Active Records,則可以使用DAO執行適當的SQL插入語句以插入多行。提前
Insert multiple records into MySQL with a single query
一旦你有你的INSERT語句作爲一個字符串(可以動態使用的情況下,一個循環來構建你不知道有多少行:你可以在這裏找到這樣的語句的例子你需要插入),你可以像這樣執行它:
$sql = 'INSERT statement goes here';
$connection = Yii::app() -> db;
$command = $connection -> createCommand($sql);
$command -> execute();
我遇到了同樣的問題。
據我所知Yii沒有插入多行的默認函數。
/**
* Creates and executes an INSERT SQL statement for several rows.
* @param string $table the table that new rows will be inserted into.
* @param array $array_columns the array of column datas array(array(name=>value,...),...) to be inserted into the table.
* @return integer number of rows affected by the execution.
*/
public function insertSeveral($table, $array_columns)
{
$sql = '';
$params = array();
$i = 0;
foreach ($array_columns as $columns) {
$names = array();
$placeholders = array();
foreach ($columns as $name => $value) {
if (!$i) {
$names[] = $this->_connection->quoteColumnName($name);
}
if ($value instanceof CDbExpression) {
$placeholders[] = $value->expression;
foreach ($value->params as $n => $v)
$params[$n] = $v;
} else {
$placeholders[] = ':' . $name . $i;
$params[':' . $name . $i] = $value;
}
}
if (!$i) {
$sql = 'INSERT INTO ' . $this->_connection->quoteTableName($table)
. ' (' . implode(', ', $names) . ') VALUES ('
. implode(', ', $placeholders) . ')';
} else {
$sql .= ',(' . implode(', ', $placeholders) . ')';
}
$i++;
}
return $this->setText($sql)->execute($params);
}
用法:
$rows = array(
array('id' => 1, 'name' => 'John'),
array('id' => 2, 'name' => 'Mark')
);
$command = Yii::app()->db->createCommand();
$command->insertSeveral('users', $rows);
更新
由於
所以我在CdbCommand
類(框架/ DB/CDbCommand.php)創建一個Nabi K.A.Z.提到它真的好多了不要觸摸框架的源代碼。 在我的項目中,我實際上創建了MyCDbCommand類(它擴展了CDbCommand),並且創建了MyCDBConnection(擴展了CDbConnection)。
MyCDbCommand(擴展CDbCommand):
class MyCDbCommand extends CDbCommand
{
protected $_connection;
public function __construct(CDbConnection $connection, $query = null)
{
$this->_connection = $connection;
parent::__construct($connection, $query);
}
/**
* Creates and executes an INSERT SQL statement for several rows.
* @param string $table the table that new rows will be inserted into.
* @param array $array_columns the array of column datas array(array(name=>value,...),...) to be inserted into the table.
* @return integer number of rows affected by the execution.
*/
public function insertSeveral($table, $array_columns)
{
$sql = '';
$params = array();
$i = 0;
foreach ($array_columns as $columns) {
$names = array();
$placeholders = array();
foreach ($columns as $name => $value) {
if (!$i) {
$names[] = $this->_connection->quoteColumnName($name);
}
if ($value instanceof CDbExpression) {
$placeholders[] = $value->expression;
foreach ($value->params as $n => $v) {
$params[$n] = $v;
}
} else {
$placeholders[] = ':' . $name . $i;
$params[':' . $name . $i] = $value;
}
}
if (!$i) {
$sql = 'INSERT INTO ' . $this->_connection->quoteTableName($table)
. ' (' . implode(', ', $names) . ') VALUES ('
. implode(', ', $placeholders) . ')';
} else {
$sql .= ',(' . implode(', ', $placeholders) . ')';
}
$i++;
}
return !empty($sql) ? $this->setText($sql)->execute($params) : 0;
}
}
MyCDBConnection(擴展CDbConnection和使用MyCDbCommand):
class MyCDbConnection extends CDbConnection
{
public function createCommand($query = null)
{
$this->setActive(true);
return new MyCDbCommand($this, $query);
}
}
然後我改變配置文件(/保護/配置/ main.php)。 我已經改變了CDbConnection到MyCDbConnection有:
...
'components'=>array(
...
'db' => array(
'connectionString' => 'mysql:host=localhost;dbname=dname',
'username' => 'user',
'password' => 'password',
'charset' => 'utf8',
'class' => 'MyCDbConnection', // Change default CDbConnection class to MyCDbConnection
),
...
)
...
在這裏你去。我們沒有觸及核心框架,而是可以像以前一樣使用它。
以上代碼從Pigalev Pavel是偉大的。
但是這需要修改核心框架,這是不好的!
所以,我寫了一個獨立的類。
將此代碼放入components
文件夾下的GeneralRepository.php
文件名。
<?php
class GeneralRepository
{
/**
* Creates and executes an INSERT SQL statement for several rows.
*
* Usage:
* $rows = array(
* array('id' => 1, 'name' => 'John'),
* array('id' => 2, 'name' => 'Mark')
*);
* GeneralRepository::insertSeveral(User::model()->tableName(), $rows);
*
* @param string $table the table that new rows will be inserted into.
* @param array $array_columns the array of column datas array(array(name=>value,...),...) to be inserted into the table.
* @return integer number of rows affected by the execution.
*/
public static function insertSeveral($table, $array_columns)
{
$connection = Yii::app()->db;
$sql = '';
$params = array();
$i = 0;
foreach ($array_columns as $columns) {
$names = array();
$placeholders = array();
foreach ($columns as $name => $value) {
if (!$i) {
$names[] = $connection->quoteColumnName($name);
}
if ($value instanceof CDbExpression) {
$placeholders[] = $value->expression;
foreach ($value->params as $n => $v)
$params[$n] = $v;
} else {
$placeholders[] = ':' . $name . $i;
$params[':' . $name . $i] = $value;
}
}
if (!$i) {
$sql = 'INSERT INTO ' . $connection->quoteTableName($table)
. ' (' . implode(', ', $names) . ') VALUES ('
. implode(', ', $placeholders) . ')';
} else {
$sql .= ',(' . implode(', ', $placeholders) . ')';
}
$i++;
}
$command = Yii::app()->db->createCommand($sql);
return $command->execute($params);
}
}
和使用的任何地方:
$rows = array(
array('id' => 1, 'name' => 'John'),
array('id' => 2, 'name' => 'Mark')
);
GeneralRepository::insertSeveral(User::model()->tableName(), $rows);
謝謝!我忘了在這裏改變我的答案,但在我的項目中,我最終還是要上一門新課。有點不同但非常相似。我已經更新了我的答案。 – 2014-08-16 17:34:58
Yii的支持添加多個記錄。
使用此....
$alertStatus[] = array(
'db_field_name1' => $value1,
'db_field_name1' => $value2,
'created_on' => new CDbExpression('NOW()'),
'modified_on' => new CDbExpression('NOW()')
);
$connection = Yii::app()->db->getSchema()->getCommandBuilder();
$command = $connection->createMultipleInsertCommand('table_name', $alertStatus);
$command->execute();
$ alertStatus數組應該包含所有數據庫
感謝它爲我工作 – 2017-08-09 10:44:22
你可以讓它成爲正面迴應 – Nitin 2017-08-09 10:44:49
的這看起來並不如Javascript等領域。我認爲這是PHP。 – hugomg 2012-01-05 22:40:06