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

[2.0] Fix upsert handling of nullable fields #1767

Merged
merged 3 commits into from
Mar 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG-1.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ CHANGELOG for 1.2.x

This changelog references the relevant changes done in 1.2 patch versions.

1.2.2 (2018-03-31)
------------------

All issues and pull requests in this release may be found under the
[1.2.2 milestone](https://github.com/doctrine/mongodb-odm/issues?q=milestone%3A1.2.2).

* [#1764](https://github.com/doctrine/mongodb-odm/pull/1764) fixes upserting documents with nullable fields.
* [#1748](https://github.com/doctrine/mongodb-odm/pull/1748) fixes the usage of references as shard keys.
* [#1732](https://github.com/doctrine/mongodb-odm/pull/1732) adds missing `cascade-detach` to the XML mapping schema.
* [#1731](https://github.com/doctrine/mongodb-odm/pull/1731) fixes some errors in the XML mapping schema.

1.2.1 (2017-12-08)
------------------

Expand Down
12 changes: 9 additions & 3 deletions lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,17 @@ 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
if (empty($data['$set'])) {
unset($data['$set']);
// Do not send empty update operators
foreach (['$set', '$inc', '$setOnInsert'] as $operator) {
if (! empty($data[$operator])) {
continue;
}

unset($data[$operator]);
}

/* If there are no modifiers remaining, we're upserting a document with
Expand Down
8 changes: 5 additions & 3 deletions lib/Doctrine/ODM/MongoDB/Persisters/PersistenceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,18 @@ public function prepareUpsertData($document)

// Scalar fields
if (! isset($mapping['association'])) {
if ($new !== null || $mapping['nullable'] === true) {
if ($new !== null && empty($mapping['id']) && isset($mapping['strategy']) && $mapping['strategy'] === ClassMetadata::STORAGE_STRATEGY_INCREMENT) {
if ($new !== null) {
if (empty($mapping['id']) && isset($mapping['strategy']) && $mapping['strategy'] === ClassMetadata::STORAGE_STRATEGY_INCREMENT) {
$operator = '$inc';
$value = Type::getType($mapping['type'])->convertToDatabaseValue($new - $old);
} else {
$operator = '$set';
$value = $new === null ? null : Type::getType($mapping['type'])->convertToDatabaseValue($new);
$value = Type::getType($mapping['type'])->convertToDatabaseValue($new);
}

$updateData[$operator][$mapping['name']] = $value;
} elseif ($mapping['nullable'] === true) {
$updateData['$setOnInsert'][$mapping['name']] = null;
}

// @EmbedOne
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public function testUpsertObject($className, $id, $discriminator)
$this->assertEquals((string) $id, (string) $check['_id']);
$this->assertEquals($group->getId(), (string) $check['groups'][0]['$id']);
$this->assertEquals($discriminator, $check['discriminator']);
$this->assertArrayHasKey('nullableField', $check);
$this->assertNull($check['nullableField']);

$group2 = new Group('Group');

Expand All @@ -78,6 +80,7 @@ public function testUpsertObject($className, $id, $discriminator)
$user->hits = 5;
$user->count = 2;
$user->groups = [$group2];
$user->nullableField = 'foo';
$this->dm->persist($user);
$this->dm->flush();
$this->dm->clear();
Expand All @@ -91,6 +94,7 @@ public function testUpsertObject($className, $id, $discriminator)
$this->assertEquals($group2->getId(), (string) $check['groups'][1]['$id']);
$this->assertArrayHasKey('username', $check);
$this->assertEquals('test', $check['username']);
$this->assertEquals('foo', $check['nullableField']);

$user = new $className();
$user->id = $id;
Expand All @@ -107,6 +111,7 @@ public function testUpsertObject($className, $id, $discriminator)
$this->assertEquals($group2->getId(), (string) $check['groups'][1]['$id']);
$this->assertArrayHasKey('username', $check);
$this->assertEquals('test', $check['username']);
$this->assertEquals('foo', $check['nullableField']);
}

public function testInheritedAssociationMappings()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public function testPrepareInsertDataWithCreatedReferenceOne()
'$id' => new ObjectId($article->id),
'$ref' => 'CmsArticle',
],
'nullableField' => null,
];
$this->assertDocumentInsertData($expectedData, $this->pb->prepareInsertData($comment));
}
Expand All @@ -171,6 +172,7 @@ public function testPrepareInsertDataWithFetchedReferenceOne()
'$id' => new ObjectId($article->id),
'$ref' => 'CmsArticle',
],
'nullableField' => null,
];
$this->assertDocumentInsertData($expectedData, $this->pb->prepareInsertData($comment));
}
Expand Down Expand Up @@ -202,6 +204,7 @@ public function testPrepareUpsertData()
],
'_id' => new ObjectId($comment->id),
],
'$setOnInsert' => ['nullableField' => null],
];
$this->assertEquals($expectedData, $this->pb->prepareUpsertData($comment));
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Documents/CmsComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class CmsComment
/** @ODM\Field(name="ip", type="string") */
public $authorIp;

/** @ODM\Field(type="string", nullable=true) */
public $nullableField;

public function setArticle(CmsArticle $article)
{
$this->article = $article;
Expand Down
3 changes: 3 additions & 0 deletions tests/Documents/UserUpsert.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ class UserUpsert

/** @ODM\ReferenceMany(targetDocument="Group", cascade={"all"}) */
public $groups;

/** @ODM\Field(type="string", nullable=true) */
public $nullableField;
}
3 changes: 3 additions & 0 deletions tests/Documents/UserUpsertIdStrategyNone.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ class UserUpsertIdStrategyNone

/** @ODM\ReferenceMany(targetDocument="Group", cascade={"all"}) */
public $groups;

/** @ODM\Field(type="string", nullable=true) */
public $nullableField;
}