O
O
Octoberfest2018-03-28 12:07:21
Yii
Octoberfest, 2018-03-28 12:07:21

How to trigger parent model event in YII2?

Hello.
The system has 2 models. For example, House and Flat.
Each House has several Flats, and each Flat has one House.
One-to-many relationship.
What I want to do: if Flat is updated, then call the update event of the corresponding House. How can this be done within YII2? Use AfterSave event and do Trigger on House?
Are there any other options?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Timofeev, 2018-03-28
@webinar

1. This is not a parent model, but a related one, for both flat and House, the parent model is AR or Model. While it's probably more correct to name the parent class, it's confusing anyway.
2. If changes to flat require changes to House, there's a good chance you don't have the right structure. Describe what exactly you want to change there. For example, if you write to the base the number of flats for house, then this is fundamentally wrong, for this a method is created that dynamically counts. Well, etc.
3. There are events in the model, including afterSave . Describe in the afterSave methodany logic, pulling the necessary House from the connection and this logic will work every time after saving flat. If there is a need to reuse logic or be able to turn it off in some cases, it is worth implementing it as a behavior .

S
Sanovskiy, 2018-03-28
@Sanovskiy

beforeSave() and afterSave() in Yii2
I hope I understand what you mean.

L
Lander, 2018-03-28
@usdglander

Have you looked here?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question