You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Proposal: Work with the related models in AR like one-entity. #69
Yes, we can use AR/DB transaction methods from Yii but I think must work by default when we work with related models/data.
And also we type same code for save related models.
My proposal:
When we wanna create/update/save/delete related models we must use eager-loading for get models:
User::find()-> with(['files'])->one();
... forms and etc.
And when we get data by request from browser we just use load function:
$user->load(Yii::$app->request->post())
And this function load all related models if it was loaded. And after that - validate & save all models like one Entity.
BUT: For security reason we must allow to work like this only for one "Entity".
What I mean - UserFiles without User doesn't have any value, User and UserFiles - it is one Entity. And for this we must have agreement that - If a model name has parent name (UserFiles has User) it must interpreted like one-single entity.
Thanks. I will be grateful for the reasoned criticism and additions.
And yes, I'm ready to implement this if it was be accepted.
UPDATE (2019-05-22):
dependent models by model name (User -> [User]File) - reject
My proposal - use static method that return array where key is relation name and value is FQCN for dependent model
Example ['files' => 'app/models/UserFile']
We should add too much hidden work behind the scene mainly to set the foreign keys, and it is not a good thing to have not full control of what happens during save.
I usually adopt the next example, simple, clear and linear.
$user = ... fill user model ...
$userFile = ... fill userFile model ... (without setting foreign key because it is not known at this time)
$transaction = \Yii::$app->db->beginTransaction();
try
{
// save the parent model (user). If it fails, an exception is thrownif($user->save())
{
// set the foreign key$userFile->user_id = $user->id;
// save the child model (userFile). If it fails, an exception is thrownif($userFile->save())
{
$transaction->commit();
}
else
{
thrownew \Exception('error on UserFile Model ' . print_r($userFile->firstErrors));
}
}
else
{
thrownew \Exception('error on User Model ' . print_r($user->firstErrors));
}
}
catch(\Exception$excp)
{
$transaction->rollback();
}
@FabrizioCaldarelli I think you agree with me that it doesn't need for app, it is just some code for AR (or DB layer) and it is repeatable and same for all dependent models.
@asamats It is not a AR responsability to do this work.
Again, think at more complex cases, where you need to "sync" models, and not only save.
For example, you start with 3 UserFile model related to 1 UserFile. Then, I want to save only 2 UserFile (so the other UserFile should be deleted). Your code in this case not work, because it will only save (without changes) the two models, without delete the other.
@asamats I'm not sure why you're closing this issue. It's labeled under discussion and other developers/community member may still not had the chance to review & think about your proposal..
by find PK. Usually we have 1-100 related models, and it's fast to compare in the loop.
using model's PK for key in the related's array.
And tagged all depend models - deletable(checked) and when updated models by incoming data - deletable(unchecked). After that it's simple to delete/update/add dependent models.
Like I have updated my proposal - it was mistake.
I don't see any problem with implementation.
Question is - want we have in AR this is functionality?
this logic mustn't be default (if it will be realized in any variant)
it is not AR responsibility at all. AR is already overloaded (I mean yii2 version). If it will be implemented it should be a separate service or at least a decorator. Maybe it is reasonable to make another composer library for this.
it could be a good kick-up for startups, but a very big headache for big old projects.
@viktorprogger "it could be a good kick-up for startups, but a very big headache for big old projects."
Did you research my draft-pull-request? It doesn't break anything and by default don't change AR behavior.
You can use this functionality when you want.
What about AR overloaded - I can't say nothing, but when you proposal to use decorator pattern - I'm confused. Do you realize how to do this? I will be happy if you push draft-pull-request.
"this logic mustn't be default" - it doesn't.
P.S. Yii never will be like Symfony. It's his best side.
Activity
fcaldarelli commentedon May 16, 2019
We should add too much hidden work behind the scene mainly to set the foreign keys, and it is not a good thing to have not full control of what happens during save.
I usually adopt the next example, simple, clear and linear.
asamats commentedon May 22, 2019
Please, look at #70 and make comments. Thanks.
asamats commentedon May 22, 2019
@FabrizioCaldarelli I think you agree with me that it doesn't need for app, it is just some code for AR (or DB layer) and it is repeatable and same for all dependent models.
fcaldarelli commentedon May 22, 2019
@asamats Sure, but I'd not include automatically this feature inside AR or DB layer.
asamats commentedon May 22, 2019
@FabrizioCaldarelli Yes, I agree about automatically feature that is way I change my proposal.
Now it is customizable.
fcaldarelli commentedon May 22, 2019
@asamats I could have dependent models, but I could want to not update them when I save the parent model.
asamats commentedon May 22, 2019
@FabrizioCaldarelli What if we add a flag or new method for save parent and all depend models (if them added and updated)?
fcaldarelli commentedon May 22, 2019
@asamats It is not a AR responsability to do this work.
Again, think at more complex cases, where you need to "sync" models, and not only save.
For example, you start with 3 UserFile model related to 1 UserFile. Then, I want to save only 2 UserFile (so the other UserFile should be deleted). Your code in this case not work, because it will only save (without changes) the two models, without delete the other.
asamats commentedon May 22, 2019
@asamats In this case it do. But anyway, I see that core yii developer doesn't approve.
Thanks for your comments, them was helpful.
machour commentedon May 22, 2019
@asamats I'm not sure why you're closing this issue. It's labeled under discussion and other developers/community member may still not had the chance to review & think about your proposal..
viktorprogger commentedon May 27, 2019
I think it's not a good idea to try to add this functionality by default to all the ARs. Just think about this cases:
new
UserFiles or find existent in DB or delete them?IMHO this logic must be implemented situationally as it is project-specific.
asamats commentedon May 27, 2019
@viktorprogger
And tagged all depend models - deletable(checked) and when updated models by incoming data - deletable(unchecked). After that it's simple to delete/update/add dependent models.
I don't see any problem with implementation.
Question is - want we have in AR this is functionality?
viktorprogger commentedon May 29, 2019
I still think
fcaldarelli commentedon May 29, 2019
@asamats why don't you create an extension to achieve it?
asamats commentedon May 29, 2019
@FabrizioCaldarelli because we have similar exts. I don't see any reason to create one other.
asamats commentedon May 31, 2019
@viktorprogger "it could be a good kick-up for startups, but a very big headache for big old projects."
Did you research my draft-pull-request? It doesn't break anything and by default don't change AR behavior.
You can use this functionality when you want.
What about AR overloaded - I can't say nothing, but when you proposal to use decorator pattern - I'm confused. Do you realize how to do this? I will be happy if you push draft-pull-request.
"this logic mustn't be default" - it doesn't.
P.S. Yii never will be like Symfony. It's his best side.