2012-10-27 11 views
5

誰もYiiの1つのフォームを使用して複数のレコードを追加する方法を知っていますか?すべてのレコードは同じモデルに属し、同じフォーマットです。Yii - 1つのフォーム提出の複数のレコード

多くのおかげで、

ニック

+2

http://www.yiiframework.com/doc/guide/1.1/ja/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コール中に検証を実行させます。したがって、ロジックは実際にあなたのアプリのuxに依存します。

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

この一つだけのクエリを実行します。

関連する問題