Answer the question
In order to leave comments, you need to log in
How to pass the id of the new record from actionCreate to afterSave?
Can you tell me how to pass id to this function?
public function actionCreate()
{
$model = new Products();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
protected function afterSave(){
parent::afterSave();
if($this->isNewRecord){
$model = Products::findOne($model->id);
$model->image = UploadedFile::getInstance($model, 'image');
if($model->image){
$model->upload();
}
}
}
Answer the question
In order to leave comments, you need to log in
Good afternoon.
protected function afterSave(){
parent::afterSave();
if($this->isNewRecord){
$model = Products::findOne($this->id);
$model->image = UploadedFile::getInstance($model, 'image');
if($model->image){
$model->upload();
}
}
}
How did you afterSave
end up with a variable $model
? From the air?
Probably $this
should use
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question