Skip to content
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

Fix related records auto-save with belongsTo() relation #15203

Closed
wants to merge 1 commit into from

Conversation

gponcon
Copy link

@gponcon gponcon commented Nov 4, 2020

Hello!

In raising this pull request, I confirm the following:

  • I have read and understood the Contributing Guidelines
  • I have checked that another pull request for this purpose does not exist
  • I wrote some tests for this PR
  • I have updated the relevant CHANGELOG
  • I have created a PR for the documentation about this change

Small description of change:

Models: with a relation belongsTo() - hasMany(), the related records need to be saved in preSaveRelatedRecords(). This fix update the dirtyState of related records to trigger related records save.

@zsilbi zsilbi self-assigned this Nov 4, 2020
@zsilbi zsilbi added 4.1.0 bug A bug report 4.1.1 The issues we want to solve in the 4.1.1 release and removed 4.1.0 labels Nov 4, 2020
@zsilbi zsilbi self-requested a review November 4, 2020 10:52
@gponcon
Copy link
Author

gponcon commented Nov 4, 2020

Note that this fix may not be perfect because when you access a linked record it seems to be saved anyway.

According to my tests:

$invoice = Invoices::findFirst();
$invoice->customer->name = "name";
$invoice->save(); // customer is saved 👍 

$invoice = Invoices::findFirst();
$invoice->save(); // customer is not saved 👍 

$invoice = Invoices::findFirst();
echo $invoice->customer->name;
$invoice->save(); // customer is saved 👎 

Maybe there is something to do in __set() or somewhere else?

@zsilbi
Copy link
Member

zsilbi commented Nov 4, 2020

$invoice = Invoices::findFirst();
echo $invoice->customer->name;
$invoice->save(); // customer is saved 👎 

Maybe there is something to do in __set() or somewhere else?

We don't have knowledge about the state of the properties in (related) models.
If the a relatation has been accessed in any way, we will have to save that record, because it could have been modified since the query.
To eliminate this, we would have to enforce using snapshots (which is not a possibility) and compare them to their current state before each action.

dirtyState does not reflect on property changes, it only indicates if the record is saved to the database.

@gponcon
Copy link
Author

gponcon commented Nov 4, 2020

$invoice = Invoices::findFirst();
$invoice->save(); // customer is not saved 👍

What do you expect to be saved for customer in this case?

customer is not saved because it hasn't been accessed, which sounds pretty good.

@gponcon
Copy link
Author

gponcon commented Nov 4, 2020

(...)
We don't have knowledge about the state of the properties in (related) models.
If the a relatation has been accessed in any way, we will have to save that record, because it could have been modified since the query.
To eliminate this, we would have to enforce using snapshots (which is not a possibility) and compare them to their current state before each action.

dirtyState does not reflect on property changes, it only indicates if the record is saved to the database.

Thank you for this clarification. Indeed, I did not know how to deal with dirtyState for this resolution.

@niden
Copy link
Member

niden commented Mar 28, 2021

@gponcon Thank you so much for the bug fix. Could you please change your target branch to be 5.0.x? We never send PRs to master unless it is a release

It is looking good

@gponcon gponcon force-pushed the fix-related-belongs-record branch from f343dd9 to 1f6af91 Compare April 1, 2021 12:10
@gponcon gponcon changed the base branch from master to 5.0.x April 1, 2021 12:20
@gponcon
Copy link
Author

gponcon commented Apr 1, 2021

@niden I modified the target branch as requested. Let me know if it is correct, I am at your disposal.

@gponcon gponcon force-pushed the fix-related-belongs-record branch from 1f6af91 to dad4fbc Compare April 1, 2021 12:34
@niden
Copy link
Member

niden commented Dec 1, 2021

@gponcon Can you please rebase to the latest 5.0.x branch and add an entry to the changelog? Then we can merge this

@niden niden mentioned this pull request Dec 23, 2021
5 tasks
@niden
Copy link
Member

niden commented Dec 23, 2021

Closing in favor of #15838. Original work preserved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.1.1 The issues we want to solve in the 4.1.1 release bug A bug report
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants