0

我的项目员工签证模型和签证跟踪模型中有两个模型。我想将员工签证表的主键(即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在保存在visa track中后被存储为Null值。我如何解决这个问题?

4

2 回答 2

0

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

在您的员工签证模型中执行以下操作:

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

于 2017-03-20T16:03:46.290 回答
0

您在 $visas 上使用 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;
于 2017-03-20T09:47:58.870 回答