D
D
Dmitry2018-10-02 20:23:31
Yii
Dmitry, 2018-10-02 20:23:31

How to call a static method on an event?

Good evening.
You need to change the status in several models.
There are models "brand", "model", "series", "modification". When you change the status ("active", "blocked") in the "brand" model, you need to change the statuses in other models.
In the gridView, the "status" column has an ajax request handler that changes the status.

public function actionUpdateStatusMark()
    {
        $id = Yii::$app->request->post('id');
        $model = CarMark::find()->where('id_car_mark=:id', [':id' => $id])->one();
        $model->status = $model->status == 0 ? 1 : 0;
        $model->save();
        return $model->status;
    }

In the "Mark" model itself, in the afterSave() method, the status in the "Model" is changed
public function afterSave($insert, $changedAttributes)
    {
        CarModel::setStatusModel($this->id_car_mark, $this->status);
        parent::afterSave($insert, $changedAttributes);
    }

This section of the code works without problems, the status of the brand changes, and the statuses of all models belonging to the selected brand change accordingly.
But for other models it is not possible to change the status.
Initially, the methods that change the status looked like this
public static function setStatusModel($id, $status)
    {
        $models = CarModel::find()->where('id_car_mark=:id', [':id' => $id])->all();
        foreach($models as $model){
            $model->status = $status;
            $model->save();
        }
    }

But getting the statuses of models, and then updating each record in a cycle seemed to me too much. Especially since an error about lack of memory flies out every other time. Therefore, I rewrote such methods as follows.
public static function setStatusModel($id, $status)
    {
        self::updateAll(['status' => $status], 'id_car_mark=:id', [':id' => $id]);
    }

Naturally, afterSave() won't work now.
I tried to create an event in the model to call status update methods on other models.
That's all... Nothing works.
It looks something like this:
in the "Model"
const AFTER_UPDATE = 'afterUpdate';
    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
        $this->on(self::AFTER_UPDATE, function (){
            CarSerie::setStatusSerie($this->id_car_model, $this->status); // меняет статус у серии.
        });
    }
    public function afterUpdate()
    {
        $this->trigger('afterUpdate');
    }

I'm trying to call in afterSave() the "Mark" model
public function afterSave($insert, $changedAttributes)
    {
        CarModel::setStatusModel($this->id_car_mark, $this->status);
        $carModel = new CarModel();
        $carModel->afterUpdate();
        parent::afterSave($insert, $changedAttributes);
    }

The statuses of the brand and model change, nothing changes for the series.
How to solve the problem correctly? Is it possible to call static methods on an event?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arman, 2018-10-02
@slo_nik

1. For good, all this needs to be started by transactions
2. You do not have Model instances, so you need to conduct your events from the new version of "setStatusModel"
3. SetStatusModel works for you on any sneeze. it is better to make an event on changes in the status field when saving
4. As an option, leave it as it was and use Batch fetch

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question