A
A
Absky2018-08-27 16:27:11
Yii
Absky, 2018-08-27 16:27:11

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

2 answer(s)
D
Dmitry, 2018-08-27
@Absky

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();
            }
        }
    }

M
Maxim Timofeev, 2018-08-27
@webinar

How did you afterSaveend up with a variable $model? From the air?
Probably $thisshould use

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question