2012-10-27 127 views
5

有谁知道如何使用Yii中的一个表单添加多个记录?所有记录属于同一个模型,格式相同。Yii - 在一个表单中提交多个记录

非常感谢,

尼克

+2

http://www.yiiframework.com/doc/guide/1.1/en/form.table – dInGd0nG

+0

谢谢!就是我在找的东西。 – goose

+0

是否有人能够确认等效的batchCreate方法的语法?这在上面链接的页面中缺少。 – goose

回答

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); 

这只是执行一个查询。