数据未存储在yii2

时间:2017-03-20 09:36:33

标签: yii2 yii2-model yii2-validation

我的项目员工签证模型和签证跟踪模型中有两个模型。我想将员工签证表的主键(即,employee_visa_id)存储在下一张表签证轨道的employee_visa_id字段中。 签证跟踪的作用是:

public function rules()
{
    return [
        [['emp_id', 'employee_passport_id', 'country_id', 'visa_configuration_id',], 'required'],
        [['emp_id', 'employee_passport_id', 'country_id', 'visa_configuration_id','employee_visa_id'], 'integer'],
        [['validity'], 'safe'],
        [['remarks'], 'string'],
    ];
} 

员工签证模式的作用是:

 public function rules()
{
    return [
        [['emp_id', 'employee_passport_id', 'country_id','visa_type', 'expiration_date'], 'required'],
        [['emp_id', 'employee_passport_id'], 'integer'],
        [['expiration_date'], 'safe'],
        [['remarks'], 'string'],
        [['visa_type'], 'string', 'max' => 300],
    ];
}

在控制器中我尝试:

    foreach ($visas as $visa):


                $visa->expiration_date = date('Y-m-d',  strtotime($visa->expiration_date));
                $visa_track->emp_id = $visa->emp_id;
                $visa_track->employee_passport_id = $visa->employee_passport_id;
                $visa_track->country_id = $visa->country_id;
                $visa_track->visa_configuration_id = $visa->visa_type;
                $visa_track->validity = $visa->expiration_date;
                $emp_vid = $visa->employee_visa_id;
                $visa_track->employee_visa_id = $visa->employee_visa_id;

                //print_r($visa_track->employee_visa_id );die();

                if($visa_track->validate())
                {
                   // print_r($visa_track->employee_visa_id );die();
                    $visa_track->save();
                }
                else
                {
                    $errors = $visa_track->errors;
                    print_r($errors);die();
                }

                $visa->save(false);
                //$visa_track->save(false);

            endforeach;

但问题是员工签证的employee_visa_id在保存在签证轨道后存储为空值。我如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

你在$签证上使用foreach然后你应该为visa_track使用等价结构...例如。在每次迭代中添加一个新对象
否则你$ visa_track-> save(); ..每次只更新相同的模型

告诉你visa_track模型是名称VisaTrack

foreach ($visas as $visa):

              $visa_track  = new VisaTrack();

              $visa->expiration_date = date('Y-m-d',  strtotime($visa->expiration_date));
              $visa_track->emp_id = $visa->emp_id;  
              $visa_track->employee_passport_id = $visa->employee_passport_id;
              $visa_track->country_id = $visa->country_id;
              $visa_track->visa_configuration_id = $visa->visa_type;
              $visa_track->validity = $visa->expiration_date;
              $emp_vid = $visa->employee_visa_id;
              $visa_track->employee_visa_id = $visa->employee_visa_id;

              //print_r($visa_track->employee_visa_id );die();

              if($visa_track->validate())
              {
                 // print_r($visa_track->employee_visa_id );die();
                  $visa_track->save();
              }
              else
              {
                  $errors = $visa_track->errors;
                  print_r($errors);die();
              }

              $visa->save(false);
              //$visa_track->save(false);

          endforeach;

答案 1 :(得分:0)

为什么不使用内置函数afterSave? 每次保存/更新后都会被触发

在您的员工VISA模型中,执行以下操作:

public function afterSave($insert, $changedAttributes)
{
    $visa_track  = new VisaTrack();
    $visa_track->emp_id = $this->emp_id;  
    $visa_track->employee_passport_id = $this->employee_passport_id;
    $visa_track->country_id = $this->country_id;
    $visa_track->visa_configuration_id = $this->visa_type;
    $visa_track->validity = $this->expiration_date;
    $visa_track->employee_visa_id = $this->employee_visa_id;

    if($visa_track->validate()){
        $visa_track->save();
    }

    parent::afterSave($insert, $changedAttributes);
}

但即便如此,您还需要将VisaTrack对象声明为已提及的scaisEdge