-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unlink and link should also use the via on conditions of the relation #17174
Comments
Update BaseActiveRecord so that link() and unlink() supports viaRelation on conditions. yiisoft#17174
I don't want to use any |
@samdark I think you meant to tag @papppeter :) |
@samdark do you think somebody from the core team could pick this up? we checked the problem and the suggested solution as well. and it is not really easy to do it, without a deep knowledge of AR. By the way the solution looks quite close to be a good one. i think this should be fixed on the long run, it is not doing the expected behaviour. or make i just deprecated so people are aware of this problem prompt. what do you think? |
Only if @bizley has enough time for it. |
Hi @bizley would you have time to look into this problem? it would be a great help to us! |
I'll look into this. |
@papppeter could you check if proposed change in |
@bizley i can do it tomorrow, latest on Monday. |
@pappfer will take some time. Meanwhile you can try using dev-master@dev. |
@bizley is there a reason why the condition wasn't also added to "link()" as well as i suggested in my PR almost 2 years ago? i mean if an relation is now correctly unlinked with "all conditions" why should this not be the same for "link" as well. Without the change i always have to set the conditions if i wanted to link between 2 models which are on a specific relation. |
@Bhoft i think that part is just less straight forward, collecting all the values if the on condition has some multi level condition. Link method has the extraColumns which you can use. we are using https://github.com/la-haute-societe/yii2-save-relations-behavior which will be now a perfect solution for this problem. by the way you can always write your link method which prefills the extraColumns. unlink you couldn’t get it to work the way it should |
Yes i knew, but to have the correct "relation" between my models i "must" also "prefill" the extra Columns. I also knew that i extend or overload the link function, but in my opinion this shouldn't be a "manual" thing to do. |
@Bhoft For additional columns info |
Ok but i don't think you would have an relation with an on condition with column > 1 or something similar for crud operations of the models, only for special listings or similar. I am fine with it, but maybe there is really some special case were it could be useful. |
Hi
I have a ManyMany relation between 2 Tables e.g. ReviewerInvites and Submissions.
I have an allocation of "multiple" reviewer Types e.g. individual review, consensus review.
Therefore i have an additional column "reviewer_type" in the junction Table.
and defined constants like :
The Junction Table has the following columns,
submission_id, reviewer_invite_id, reviewer_type
I could now have the relation in my Submission Model like this:
the same for the individual reviewers
Everything works except "link()" and "unlink()".
For "link()" i could use the "extraColumns" attribute to set the reviewer_type.
But when "unlink()" is used the On condition isn't used.
The SQL always deletes me entries for both reviewer_types:
e.g.
instead of:
I also tried to use viaTable instead of via and set the onCondition directly.
Same result.
The additional attribute must be set via extraColumns for link() but for unlink() again the onCondition isn't used. And there is no other way then to create an manual find()->where()->delete() request.
Which lets you to the point that several ManyMany releation extensions couldn't been used in this case all which uses unlink() for deleting the relations.
E.g.
la-haute-societe/yii2-save-relations-behavior#16
https://github.com/yii2tech/ar-linkmany
So I wonder why the viaRelation on and where conditions aren't used in e.g. unlink()?
https://github.com/yiisoft/yii2/blob/master/framework/db/BaseActiveRecord.php#L1408Where something like (after the line above) below would solve this issue:
Edit:
correct position is above these line because otherwise the key would go into the nulls array.
https://github.com/yiisoft/yii2/blob/master/framework/db/BaseActiveRecord.php#L1413
Something similar should also be done for link() of course.
I mean if i link between some models where some additional condition is used this should be a default value of the entry.
And if you unlink no other then the related entries should be deleted.
I have seen that this kind of request was done multiple times:
#14598
#4932
Additional info
The text was updated successfully, but these errors were encountered: