Skip to content

Fix upserts for documents with YAML mapping #1440

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

Merged
merged 1 commit into from
Jun 22, 2016
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
4 changes: 2 additions & 2 deletions lib/Doctrine/ODM/MongoDB/Persisters/PersistenceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function prepareUpdateData($document)
if ($new === null && $mapping['nullable'] !== true) {
$updateData['$unset'][$mapping['name']] = true;
} else {
if ($new !== null && $mapping['strategy'] === ClassMetadataInfo::STORAGE_STRATEGY_INCREMENT) {
if ($new !== null && isset($mapping['strategy']) && $mapping['strategy'] === ClassMetadataInfo::STORAGE_STRATEGY_INCREMENT) {
Copy link
Member

Choose a reason for hiding this comment

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

I believe this line is fixing #1435 but I don't think this is covered by tests you added?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah. I see that now. I'll update the test, thanks!

Copy link
Member Author

Choose a reason for hiding this comment

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

Test updated - it now checks to ensure IDs are not updated, since all other fields will have a mapping set, regardless of the driver being used.

$operator = '$inc';
$value = Type::getType($mapping['type'])->convertToDatabaseValue($new - $old);
} else {
Expand Down Expand Up @@ -244,7 +244,7 @@ public function prepareUpsertData($document)
// Scalar fields
if ( ! isset($mapping['association'])) {
if ($new !== null || $mapping['nullable'] === true) {
if ($new !== null && $mapping['strategy'] === ClassMetadataInfo::STORAGE_STRATEGY_INCREMENT) {
if ($new !== null && empty($mapping['id']) && isset($mapping['strategy']) && $mapping['strategy'] === ClassMetadataInfo::STORAGE_STRATEGY_INCREMENT) {
$operator = '$inc';
$value = Type::getType($mapping['type'])->convertToDatabaseValue($new - $old);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Doctrine\ODM\MongoDB\Tests\GH1435Document:
collection: documents
fields:
id:
id: true
name:
type: string
nullable: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Doctrine\ODM\MongoDB\Tests\GH1435DocumentIncrement:
collection: documents
fields:
id:
id: true
strategy: increment
name:
type: string
nullable: true
83 changes: 83 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH1435Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Doctrine\ODM\MongoDB\Tests;

use Doctrine\ODM\MongoDB\Mapping\Driver\YamlDriver;

class GH1435Test extends BaseTest
{
public function testUpsert()
{
$id = (string) new \MongoId();

$document = new GH1435Document();
$document->id = $id;
$document->name = 'test';

$this->dm->persist($document);
$this->dm->flush();
$this->dm->clear();

$document = $this->dm->find(GH1435Document::class, $id);
$this->assertNotNull($document);
$this->assertEquals('test', $document->name);
}

public function testUpsertWithIncrement()
{
$id = 10;

$document = new GH1435DocumentIncrement();
$document->id = $id;
$document->name = 'test';

$this->dm->persist($document);
$this->dm->flush();
$this->dm->clear();

$document = $this->dm->find(GH1435DocumentIncrement::class, $id);
$this->assertNotNull($document);
$this->assertEquals('test', $document->name);
}

public function testUpdateWithIncrement()
{
$document = new GH1435DocumentIncrement();
$document->name = 'test';

$this->dm->persist($document);
$this->dm->flush($document);
$this->dm->clear();

$document = $this->dm->getRepository(GH1435DocumentIncrement::class)->findOneBy([]);
$this->assertNotNull($document);
$this->assertEquals('test', $document->name);

$document->id += 5;
$this->dm->flush();
$this->dm->clear();

$document = $this->dm->getRepository(GH1435DocumentIncrement::class)->findOneBy([]);
$this->assertNotNull($document);
$this->assertSame(1, $document->id);
}

protected function createMetadataDriverImpl()
{
return new YamlDriver(__DIR__ . '/GH1435');
}
}

class GH1435Document
{
public $id;

public $name;
}

class GH1435DocumentIncrement
{
public $id;

public $name;
}