Skip to content

Fix changeset handling for new documents #999

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

Closed
wants to merge 3 commits into from
Closed
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
6 changes: 4 additions & 2 deletions lib/Doctrine/ODM/MongoDB/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,9 @@ private function computeOrRecomputeChangeSet(ClassMetadata $class, $document, $r
: $changeSet;

$this->originalDocumentData[$oid] = $actualData;
$this->documentUpdates[$oid] = $document;
if ( ! isset($this->documentInsertions[$oid])) {
Copy link
Member

Choose a reason for hiding this comment

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

Should we check $this->documentUpserts[$oid] as well here?

$this->documentUpdates[$oid] = $document;
}
}
}

Expand Down Expand Up @@ -831,7 +833,7 @@ private function computeOrRecomputeChangeSet(ClassMetadata $class, $document, $r
$oid2 = spl_object_hash($obj);
if (isset($this->documentChangeSets[$oid2])) {
$this->documentChangeSets[$oid][$mapping['fieldName']] = array($value, $value);
if ( ! $isNewDocument) {
if ( ! $isNewDocument && ! isset($this->documentInsertions[$oid])) {
$this->documentUpdates[$oid] = $document;
}
break;
Expand Down
69 changes: 69 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH999Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket;

use Doctrine\ODM\MongoDB\Event\OnFlushEventArgs;
use Doctrine\ODM\MongoDB\Events;
use Doctrine\ODM\MongoDB\Tests\BaseTest;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

class GH999Test extends BaseTest
{
public function testModifyingInFlushHandler()
{
$this->dm->getEventManager()->addEventListener(array(Events::onFlush), new GH999Listener());

$document = new GH999Document('name');
$this->dm->persist($document);
$this->dm->flush();
}
}

class GH999Listener
{
public function onFlush(OnFlushEventArgs $args) {
$dm = $args->getDocumentManager();

foreach ($dm->getUnitOfWork()->getScheduledDocumentInsertions() as $document) {
$document->setName('name #changed');
$metadata = $dm->getClassMetadata(get_class($document));
$dm->getUnitOfWork()->recomputeSingleDocumentChangeSet($metadata, $document);
}
}
}

/** @ODM\Document @ODM\HasLifecycleCallbacks */
class GH999Document
{
/** @ODM\Id */
private $id;

/** @ODM\String */
private $name;

public function __construct($name)
{
$this->name = $name;
}

public function getId()
{
return $this->id;
}

public function getName()
{
return $this->name;
}

public function setName($name)
{
$this->name = $name;
}

/** @ODM\PostUpdate */
public function postUpdate()
{
throw new \Exception('Did not expect postUpdate to be called when persisting a new document');
}
}
14 changes: 7 additions & 7 deletions tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,22 +347,22 @@ public function getScheduleForUpdateWithArraysTests()
array(
null,
array('bar' => 'foo'),
true
false
),
array(
array('foo' => 'bar'),
null,
true
false
),
array(
array('foo' => 'bar'),
array('bar' => 'foo'),
true
false
),
array(
array('foo' => 'bar'),
array('foo' => 'foo'),
true
false
),
array(
array('foo' => 'bar'),
Expand All @@ -372,17 +372,17 @@ public function getScheduleForUpdateWithArraysTests()
array(
array('foo' => 'bar'),
array('foo' => true),
true
false
),
array(
array('foo' => 'bar'),
array('foo' => 99),
true
false
),
array(
array('foo' => 99),
array('foo' => true),
true
false
),
array(
array('foo' => true),
Expand Down