Open RomeroMsk opened 9 years ago
Any thoughts?
@RomeroMsk
Now to make this work we need to write all this logic in Controller
From MVC point of view this way is wrong always.
Or another way is to override load(), validate(), create(), update() (and maybe another methods) of parent model to work with related models.
Just use correct thick models appoach. If differs from overriding methods. You can use event wrappers to make code cleaner.
Also, workflow may be different, you describe just specific application case this is why universal solution is impossible. Transactions part for example is absollute wrong.
@creocoder, so you think, we can't make any universal mechanism even for part of this?
@RomeroMsk Its already there for everything except loadWithRelations()
by using correct thik models + relations setters. Last impossible because model may have 2+ relations with same model. Do you need example?
@creocoder, yes, it will be useful.
@creocoder, could you provide an example code?
@RomeroMsk Will provide little later. Sorry, busy on work. If you want you can add my to Skype (creocoder) and we can talk about that issue.
The main problem I have for now is showing errors of related models. For example, in REST controller I need to pass these errors into response array. I can't find any native framework methods for this, so decide to attach errors of related models to main model. Any other variants?
related to #569
I suggest to add functionality of loading, validating and saving related models when calling appropriate methods. Similar to #6664, but more complex. Let's say, we have models Order and Item, where Order has many Items. When loading data of Order from form or request body (REST), we want to load also its Items. Then we need to validate both Order and Items and pass errors to form or REST response. And finally save (create or update) all of them, using transactions. I think, it's common case and many devs encountered it.
Now to make this work we need to write all this logic in Controller (
actionCreate
andactionUpdate
) or in REST action classes:$order->load()
.Item::loadMultiple()
for an array or call$item->load()
for each Item from request.validate()
for Order andvalidate()
for each Item orvalidateMultiple()
for all models.save()
for all models.Or another way is to override
load()
,validate()
,create()
,update()
(and maybe another methods) of parent model to work with related models. It is better, because we can have several places, where Orders are creating: frontend, backend, REST.So we can make universal methods for all this routine to make user code easy and clear:
$order->load()
(here we can call another method likeloadWithRelations()
or make optional parameter to not deal with relations by default). We can use built-inActiveRecord::transactions()
mechanism here or create transaction forcibly.$order->save()
(which will call$order->validate()
internally).