B
B
bizzonaru2016-08-09 17:14:24
Yii
bizzonaru, 2016-08-09 17:14:24

Yii2 How to correctly make next moment in beforeSave()?

When saving a model, knowing its id, you need to make changes in other tables. For the case when a record (model) already exists, you can do this, but how to implement it for a new one?

public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {

            $post = Yii::$app->request->post();
            
            if($this->isNewRecord) {
                // здесь это модель не существует, но надо как то тоже вызвать метод $this->UpdateLanguages($post); 
            } else {
                
                $this->UpdateLanguages($post); // Здесь функция добавляет для модели с id  список поддерживаемых языков, в частности из формы выбирается список с несколькими языками.

            }
            

            
            return true;
        }
        return false;
   }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
NosFire, 2016-08-09
@NosFire

In beforeSave() there is no entry in the database and there is no ID, respectively, either predict the ID yourself, or look towards afterSave()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question