-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Relation conditions aren't being applied when saving models #4952
Comments
This issue will be closed and archived in 3 days, as there has been no activity in the last 30 days. |
Ping |
This issue will be closed and archived in 3 days, as there has been no activity in the last 30 days. |
Ping |
This issue will be closed and archived in 3 days, as there has been no activity in the last 60 days. |
Ping |
@Flynsarmy I was able to reproduce this using your relationtest plugin. Two questions:
Note: I'm not saying this is not a bug, just exploring alternate options in the mean time. |
Update: I tried using scopes as following (same behavior as with conditions):
And adding the scopes to the Term class:
|
@LukeTowers I confirm this IS a bug, please update the Thanks. |
Fixes: octobercms/october#4952 Co-authored-by: Luke Towers <github@luketowers.ca> Co-authored-by: Ben Thomson <git@alfreido.com>
Description:
If you have two belongsToMany relations on the same table with a
conditions
value set, record retrieval (
$record->myRelation
) works correctly and appliesthe specified conditions, however saving
$record
wiil result in the firstrelation being wiped and only the second relation will save.
newPivotQuery()
isn't applying constraints as it should.Steps To Reproduce:
git clone https://github.com/Flynsarmy/oc-relationtest-plugin.git
to /plugins/flynsarmy/relationtestphp artisan plugin:refresh Flynsarmy.RelationTest
conditions
option in our model.Issue in Depth
In our
Post
model we have two relations defined:These link to the same table but retrieval and display on our form works as
expected thanks to our
conditions
statement. This can be further be confirmedwith
When saving the model,
October\Rain\Database\Relations\BelongsToMany::setSimpleValue()
calls
$this->sync($value);
.The
sync
method defined in /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.phphas the lines
to determine which records need to be saved for the current relation. The problem is
newPivotQuery()
doesn't apply ourconditions
, so the queryis executed and ALL records in this table are returned instead of just the ones
relevent to the current relation.
The next lines in this class:
detect which records weren't selected on our form and lists them for removal.
Because our form has the Categories field first, it marks the tag records for removal.
Removal occurs, and we move on to the Tags field. The same thing happens and our
categories are marked for removal.
The end result is only the tags are saved and all categories are removed.
The text was updated successfully, but these errors were encountered: