在yii2中保存多个记录

时间:2017-02-09 14:06:00

标签: php yii2 yii2-advanced-app

我有一个数组但是在保存记录时只保存了最后一条记录

这是我的代码

        if (isset($arr["transporters"])) {

           foreach ($arr["transporters"] as $other) {
                $model->company_name = $other["transportername"];
                if($model->save()){
                $allsaved = true;
                 }

            }
            if($allsaved){
                return ['data' => "Successifully created"];
            }else{
                return ['data' => "Sorry an error occured when saving the transporters"];
            }
        }

var_dump($arr)

返回

array(1) {
 ["transporters"]=>
 array(2) {
  [0]=>
   array(1) {
    ["transportername"]=> string(2) "news"  //its not saved
  }
  [1]=>
   array(1) {
     ["transportername"]=> string(4) "event"  //only this one gets saved
   }
 }
}

为什么我不能保存多条记录

1 个答案:

答案 0 :(得分:2)

正确添加适当的新模型和popuplate

import pipes
input_string = "python sub_master_script.py {} {}\n".format(*(map(pipes.quote,[str(i),path])))

p = subprocess.Popen(['qsub','-pe','make','5','-N','Teaching_{}'.format(i),'-cwd'],stdin=subprocess.PIPE)
out,err = p.communicate(input_string)