Repository navigation
Fix save() leaving the model dirty after an insert - #392
Merged
Merged
Conversation
4 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request Checklist
Description
After a model was inserted,
save()never syncedoriginalAttributes, so the model still looked entirely new. The update branch ofsave()andfind()both sync it, which made a freshly inserted model the one case that stayed dirty.Impact
create(),firstOrCreate(),updateOrCreate(),createFromModel()and anynew Modelfollowed bysave()all go through this path. After the insert:getDirtyAttributes()reported every attribute, including the primary key, andgetOriginal()returnednull.save()on the same instance ran anUPDATEthat rewrote every column (andupdated_at) with the values it had just inserted. It also firedafter_updatedand the property watchers for a row that hadn't changed.A common trigger is
$post = Post::create([...]); $post->tags()->relate(...); $post->save();, or any model saved twice in one request.Changes
save()now syncsoriginalAttributesafter a successful insert, including when hooks are suppressed withwithoutHook().after_created, so hooks see the same model state as before. This matches the update branch.EntityModelHookTest. They cover a clean model aftersave(),create(), quiet inserts,firstOrCreate()andupdateOrCreate(). They also check that asave()right after an insert fires noafter_updatedand doesn't rewrite the row, that later edits are the only dirty attributes, thatafter_createdbehavior is unchanged, and that inserted models don't share state.Backward Compatibility
Hook and watcher behavior during
after_createdis unchanged. The only difference is that an inserted model is no longer reported dirty. Code that relied on the stale dirty state to write the same values again would stop doing so, which is the intended fix.Checklist