5
A
回答
9
的"batchUpdate" from the guide相当于 “batchCreate” 的方法可能是这样的:
public function actionBatchCreate() {
$models=array();
// since you know how many models
$i=0;
while($i<5) {
$models[]=Modelname::model();
// you can also allocate memory for the model with `new Modelname` instead
// of assigning the static model
}
if (isset($_POST['Modelname'])) {
$valid=true;
foreach ($_POST['Modelname'] as $j=>$model) {
if (isset($_POST['Modelname'][$j])) {
$models[$j]=new Modelname; // if you had static model only
$models[$j]->attributes=$model;
$valid=$models[$j]->validate() && $valid;
}
}
if ($valid) {
$i=0;
while (isset($models[$i])) {
$models[$i++]->save(false);// models have already been validated
}
// anything else that you want to do, for example a redirect to admin page
$this->redirect(array('modelname/admin'));
}
}
$this->render('batch-create-form',array('models'=>$models));
}
这里唯一担心的是,一个新的实例有将使用new
为您要保存的每个型号创建。其余的逻辑可以用你希望的任何方式实现,例如在上面的例子中,所有的模型都被验证,然后保存,而如果任何模型无效,或者可以直接保存模型,则可以停止验证,在save
呼叫期间让验证发生。所以逻辑将取决于你的应用程序的用户名。
+1
感谢您的回复,并对此抱歉。这确实有帮助。 – goose
0
将此代码放入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);
这只是执行一个查询。
相关问题
- 1. 在表单提交中插入多条记录 - Yii
- 2. 在一个按钮中提交两个表单 - Yii框架
- 3. Rails - 复制表单的一部分以提交多个记录
- 4. 在单个提交中更新多个记录? - Rails的3
- 5. 当提交多个表单时,jQuery仅提交一个表单
- 6. 如何在单一提交中提交多个表单
- 7. 使用一个提交按钮将记录从一个表单提交到多个sql表格
- 8. 多个登录表单提交到一个登录操作
- 9. 在javascript中提交多个表单,单个提交按钮
- 10. 如何在yii表单中添加一个类按钮提交
- 11. 多个表单 - 一个提交按钮
- 12. 作为一个提交多个表单
- 13. 多个提交按钮。一个表单
- 14. 在单个提交中创建多条记录
- 15. 如何在另一个表单中提交表单值提交
- 16. 提交多个表单提交
- 17. 提交多个表单提交值
- 18. 用一个提交按钮提交多个表单
- 19. 多个表单提交与一个提交
- 20. 提交多个动态生成的表单与一个提交
- 21. Rails - 用一个提交按钮提交多个表单
- 22. .Net将一个大表单提交打包成多个提交
- 23. 多个表单提交
- 24. Reactjs多个表单提交
- 25. jQuery多个表单提交
- 26. Rails 3 - 如何在单一表单上动态复制记录并提交多个记录
- 27. 如何用单一提交提交多个表单?
- 28. Ajax表单在yii框架中提交
- 29. 如何在单个提交表单上提交多个提交操作?
- 30. Rails - Simple_fields_for不提交多个记录
http://www.yiiframework.com/doc/guide/1.1/en/form.table – dInGd0nG
谢谢!就是我在找的东西。 – goose
是否有人能够确认等效的batchCreate方法的语法?这在上面链接的页面中缺少。 – goose