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 more re-used embedded docs issues #1550

Merged
merged 1 commit into from
Jan 10, 2017
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
19 changes: 9 additions & 10 deletions lib/Doctrine/ODM/MongoDB/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -745,16 +745,6 @@ private function computeOrRecomputeChangeSet(ClassMetadata $class, $document, $r
if ($orgValue !== null) {
$this->scheduleOrphanRemoval($orgValue);
}

if ($actualValue !== null) {
list(, $knownParent, ) = $this->getParentAssociation($actualValue);
if ($knownParent && $knownParent !== $document) {
$actualValue = clone $actualValue;
$class->setFieldValue($document, $class->fieldMappings[$propName]['fieldName'], $actualValue);
$this->setOriginalDocumentProperty(spl_object_hash($document), $class->fieldMappings[$propName]['fieldName'], $actualValue);
}
}

$changeSet[$propName] = array($orgValue, $actualValue);
continue;
Copy link
Member Author

Choose a reason for hiding this comment

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

This is #1542 removed because it was good only for updates (it's in the path when document is not new) while one of tests I've added was interested in fixing inserts

}
Expand Down Expand Up @@ -998,6 +988,10 @@ private function computeAssociationChanges($parentDocument, array $assoc, $value
if ($assoc['type'] === ClassMetadata::ONE) {
$class->setFieldValue($parentDocument, $assoc['fieldName'], $entry);
$this->setOriginalDocumentProperty(spl_object_hash($parentDocument), $assoc['fieldName'], $entry);
$poid = spl_object_hash($parentDocument);
if (isset($this->documentChangeSets[$poid][$assoc['fieldName']])) {
$this->documentChangeSets[$poid][$assoc['fieldName']][1] = $entry;
Copy link
Member Author

Choose a reason for hiding this comment

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

This fixed #1542's failing test case and new one with early persist

}
} else {
// must use unwrapped value to not trigger orphan removal
$unwrappedValue[$key] = $entry;
Expand Down Expand Up @@ -2193,6 +2187,11 @@ function ($assoc) { return $assoc['isCascadePersist']; }
}
} elseif ($relatedDocuments !== null) {
if ( ! empty($mapping['embedded'])) {
list(, $knownParent, ) = $this->getParentAssociation($relatedDocuments);
if ($knownParent && $knownParent !== $document) {
$relatedDocuments = clone $relatedDocuments;
$class->setFieldValue($document, $mapping['fieldName'], $relatedDocuments);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This however I fail to see why I need to bring back but without this change set test with late persist is failing for reason I don't see yet

$this->setParentAssociation($relatedDocuments, $mapping, $document, $mapping['fieldName']);
}
$this->doPersist($relatedDocuments, $visited);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class GH1525Test extends \Doctrine\ODM\MongoDB\Tests\BaseTest
{
public function testEmbedClone()
public function testEmbedCloneTwoFlushesPerDocument()
{
$embedded = new GH1525Embedded('embedded');

Expand Down Expand Up @@ -38,6 +38,56 @@ public function testEmbedClone()
$this->assertSame($test->embedMany[0]->name, $embedMany->name);
}
}

public function testEmbedCloneWithIdStrategyNoneOnParentAndEarlyPersist()
{
$uuidGen = new \Doctrine\ODM\MongoDB\Id\UuidGenerator();
$embedded = new GH1525Embedded('embedded');

$count = 2;
for ($i = 0; $i < $count; ++$i) {
$parent = new GH1525DocumentIdStrategyNone($uuidGen->generateV4(), 'test' . $i);
$this->dm->persist($parent);
$parent->embedded = $embedded;
$this->dm->flush();
}

$this->dm->clear();

for ($i = 0; $i < $count; ++$i) {
$test = $this->dm->getRepository(GH1525DocumentIdStrategyNone::class)->findOneBy(array('name' => 'test' . $i));

$this->assertInstanceOf(GH1525DocumentIdStrategyNone::class, $test);

$this->assertInstanceOf(GH1525Embedded::class, $test->embedded);
$this->assertSame($test->embedded->name, $embedded->name);
}
}

public function testEmbedCloneWithIdStrategyNoneOnParentAndLatePersist()
{
$uuidGen = new \Doctrine\ODM\MongoDB\Id\UuidGenerator();
$embedded = new GH1525Embedded('embedded');

$count = 2;
for ($i = 0; $i < $count; ++$i) {
$parent = new GH1525DocumentIdStrategyNone($uuidGen->generateV4(), 'test' . $i);
$parent->embedded = $embedded;
$this->dm->persist($parent);
$this->dm->flush();
}

$this->dm->clear();

for ($i = 0; $i < $count; ++$i) {
$test = $this->dm->getRepository(GH1525DocumentIdStrategyNone::class)->findOneBy(array('name' => 'test' . $i));

$this->assertInstanceOf(GH1525DocumentIdStrategyNone::class, $test);

$this->assertInstanceOf(GH1525Embedded::class, $test->embedded);
$this->assertSame($test->embedded->name, $embedded->name);
}
}
}

/** @ODM\Document(collection="document_test") */
Expand All @@ -62,6 +112,24 @@ public function __construct($name)
}
}

/** @ODM\Document(collection="document_test_with_auto_ids") */
class GH1525DocumentIdStrategyNone
{
/** @ODM\Id(strategy="NONE") */
public $id;

/** @ODM\Field(type="string") */
public $name;

/** @ODM\EmbedOne(targetDocument="GH1525Embedded") */
public $embedded;

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

/** @ODM\EmbeddedDocument */
class GH1525Embedded
Expand Down