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 upsert handling of nullable fields #1764

Merged
merged 2 commits into from
Mar 29, 2018

Conversation

alcaeus
Copy link
Member

@alcaeus alcaeus commented Mar 27, 2018

Based on the feedback on #1761, the handling for upserting fields with a null value is correct: null values should never overwrite pre-existing data in upsert queries, since the document being upserted is in an unknown state and we can't know for certain that the value should be unset.

This PR brings this same behavior to fields with the nullable attribute set, where an upsert would always overwrite database values with a null value: this is now only done if the upsert results in an insertion, thus preserving pre-existing data but ensuring the normal behavior of the nullable flag is preserved.

This brings consistency to the upsert handling of fields with a null values. Non-nullable fields were not unset from the database to prevent loss of data, but nullable fields had their null value set in the daatabase. Since the object being upserted may be an incomplete object that doesn't completely represent the database state, we only write null values to the database if the upsert results in a document insertion.
@alcaeus alcaeus added the Bug label Mar 27, 2018
@alcaeus alcaeus added this to the 1.2.2 milestone Mar 27, 2018
@alcaeus alcaeus self-assigned this Mar 27, 2018
@alcaeus alcaeus requested a review from malarzm March 27, 2018 08:34
Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion to improve a comment, but LGTM otherwise.

@@ -329,11 +329,15 @@ private function executeUpsert($document, array $options)

foreach (array_keys($criteria) as $field) {
unset($data['$set'][$field]);
unset($data['$inc'][$field]);
unset($data['$setOnInsert'][$field]);
}

// Do not send an empty $set modifier
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should be updated to "Do not send empty update operators"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, changed it 👍

@alcaeus alcaeus force-pushed the fix-upsert-nullable-fields branch from 805f5ef to 151f30d Compare March 28, 2018 04:41
@malarzm
Copy link
Member

malarzm commented Mar 29, 2018

Merge time! Thanks @alcaeus

@malarzm malarzm merged commit 2124d2c into doctrine:1.2.x Mar 29, 2018
@alcaeus alcaeus deleted the fix-upsert-nullable-fields branch March 31, 2018 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants