Answer the question
In order to leave comments, you need to log in
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;
}
public function afterSave($insert, $changedAttributes)
{
CarModel::setStatusModel($this->id_car_mark, $this->status);
parent::afterSave($insert, $changedAttributes);
}
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();
}
}
public static function setStatusModel($id, $status)
{
self::updateAll(['status' => $status], 'id_car_mark=:id', [':id' => $id]);
}
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');
}
public function afterSave($insert, $changedAttributes)
{
CarModel::setStatusModel($this->id_car_mark, $this->status);
$carModel = new CarModel();
$carModel->afterUpdate();
parent::afterSave($insert, $changedAttributes);
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question