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

[11.x] Add "previous" state to models #53901

Draft
wants to merge 1 commit into
base: 11.x
Choose a base branch
from

Conversation

dshafik
Copy link
Contributor

@dshafik dshafik commented Dec 15, 2024

This change make it possible to retrieve the previously saved state (i.e. what original was before update/save). This gives you the ability to do similar checks as with current vs original state after save.

This adds ->getPrevious($key = null, $default = null) and ->getRawPrevious($key = null, $default = null), as well as syncPrevious() but it might be better to make the latter protected.

I also noticed a delete test in the EloquentUpdateTest so I moved it to the EloquentDeleteTest and added a correct test case in EloquentUpdateTest in its place.

@chu121su12
Copy link
Contributor

Attempted at v9 (#42504). Still a nice to have in core while being an optional trait.

@devfrey
Copy link
Contributor

devfrey commented Dec 15, 2024

This has the potential to greatly increase memory utilization for large models and should therefore be opt-in, as suggested in the previous attempt.

@shaedrich
Copy link
Contributor

Would it make sense to have a method to revert the model to the previous state?

@crynobone crynobone marked this pull request as draft December 16, 2024 05:21
@crynobone
Copy link
Member

Mark PR as a draft since tests are failing.

@donnysim
Copy link
Contributor

donnysim commented Dec 16, 2024

Previous state is really important when working spread out model actions as it becomes extremely hard to track changes. An alternative way, which I prefer and do on my base model, is to disable model automatic "original" syncing and call it manually (which in 99% cases you'll never do or need, though this may have a side effect if you call save many times on the same model) which allows to always access the original data from the moment the model was fetched. Maybe it could also be done as an opt in, which should be easier than tracking previous.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants