Skip to content

Commit 13ce687

Browse files
committed
Handle persistence for documents from views
1 parent 7af8d24 commit 13ce687

File tree

4 files changed

+51
-17
lines changed

4 files changed

+51
-17
lines changed

lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php

+4
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,10 @@ public function setCustomRepositoryClass(?string $repositoryClassName) : void
659659
*/
660660
public function invokeLifecycleCallbacks(string $event, object $document, ?array $arguments = null) : void
661661
{
662+
if ($this->isView()) {
663+
return;
664+
}
665+
662666
if (! $document instanceof $this->name) {
663667
throw new InvalidArgumentException(sprintf('Expected document class "%s"; found: "%s"', $this->name, get_class($document)));
664668
}

lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function __construct(
127127
$this->class = $class;
128128
$this->cp = $this->uow->getCollectionPersister();
129129

130-
if ($class->isEmbeddedDocument || $class->isQueryResultDocument || $class->isView()) {
130+
if ($class->isEmbeddedDocument || $class->isQueryResultDocument) {
131131
return;
132132
}
133133

lib/Doctrine/ODM/MongoDB/UnitOfWork.php

+29-9
Original file line numberDiff line numberDiff line change
@@ -450,22 +450,30 @@ private function getClassesForCommitAction(array $documents, bool $includeEmbedd
450450
if (empty($documents)) {
451451
return [];
452452
}
453+
453454
$divided = [];
454455
$embeds = [];
455456
foreach ($documents as $oid => $d) {
456457
$className = get_class($d);
457458
if (isset($embeds[$className])) {
458459
continue;
459460
}
461+
460462
if (isset($divided[$className])) {
461463
$divided[$className][1][$oid] = $d;
462464
continue;
463465
}
466+
464467
$class = $this->dm->getClassMetadata($className);
465468
if ($class->isEmbeddedDocument && ! $includeEmbedded) {
466469
$embeds[$className] = true;
467470
continue;
468471
}
472+
473+
if ($class->isView()) {
474+
continue;
475+
}
476+
469477
if (empty($divided[$class->name])) {
470478
$divided[$class->name] = [$class, [$oid => $d]];
471479
} else {
@@ -485,7 +493,7 @@ private function computeScheduleInsertsChangeSets() : void
485493
{
486494
foreach ($this->documentInsertions as $document) {
487495
$class = $this->dm->getClassMetadata(get_class($document));
488-
if ($class->isEmbeddedDocument) {
496+
if ($class->isEmbeddedDocument || $class->isView()) {
489497
continue;
490498
}
491499

@@ -502,7 +510,7 @@ private function computeScheduleUpsertsChangeSets() : void
502510
{
503511
foreach ($this->documentUpserts as $document) {
504512
$class = $this->dm->getClassMetadata(get_class($document));
505-
if ($class->isEmbeddedDocument) {
513+
if ($class->isEmbeddedDocument || $class->isView()) {
506514
continue;
507515
}
508516

@@ -611,6 +619,10 @@ public function computeChangeSet(ClassMetadata $class, object $document) : void
611619
*/
612620
private function computeOrRecomputeChangeSet(ClassMetadata $class, object $document, bool $recompute = false) : void
613621
{
622+
if ($class->isView()) {
623+
return;
624+
}
625+
614626
$oid = spl_object_hash($document);
615627
$actualData = $this->getDocumentActualData($document);
616628
$isNewDocument = ! isset($this->originalDocumentData[$oid]);
@@ -813,7 +825,7 @@ public function computeChangeSets() : void
813825
// Compute changes for other MANAGED documents. Change tracking policies take effect here.
814826
foreach ($this->identityMap as $className => $documents) {
815827
$class = $this->dm->getClassMetadata($className);
816-
if ($class->isEmbeddedDocument) {
828+
if ($class->isEmbeddedDocument || $class->isView()) {
817829
/* we do not want to compute changes to embedded documents up front
818830
* in case embedded document was replaced and its changeset
819831
* would corrupt data. Embedded documents' change set will
@@ -1280,7 +1292,7 @@ public function isScheduledForSynchronization(object $document) : bool
12801292
*
12811293
* @internal
12821294
*/
1283-
public function scheduleForDelete(object $document) : void
1295+
public function scheduleForDelete(object $document, bool $isView = false) : void
12841296
{
12851297
$oid = spl_object_hash($document);
12861298

@@ -1307,6 +1319,10 @@ public function scheduleForDelete(object $document) : void
13071319
return;
13081320
}
13091321

1322+
if ($isView) {
1323+
return;
1324+
}
1325+
13101326
$this->documentDeletions[$oid] = $document;
13111327
}
13121328

@@ -1559,7 +1575,7 @@ public function containsId($id, string $rootClassName) : bool
15591575
public function persist(object $document) : void
15601576
{
15611577
$class = $this->dm->getClassMetadata(get_class($document));
1562-
if ($class->isMappedSuperclass || $class->isQueryResultDocument || $class->isView()) {
1578+
if ($class->isMappedSuperclass || $class->isQueryResultDocument) {
15631579
throw MongoDBException::cannotPersistMappedSuperclass($class->name);
15641580
}
15651581
$visited = [];
@@ -1592,7 +1608,7 @@ private function doPersist(object $document, array &$visited) : void
15921608
switch ($documentState) {
15931609
case self::STATE_MANAGED:
15941610
// Nothing to do, except if policy is "deferred explicit"
1595-
if ($class->isChangeTrackingDeferredExplicit()) {
1611+
if ($class->isChangeTrackingDeferredExplicit() && ! $class->isView()) {
15961612
$this->scheduleForSynchronization($document);
15971613
}
15981614
break;
@@ -1601,6 +1617,10 @@ private function doPersist(object $document, array &$visited) : void
16011617
throw MongoDBException::cannotPersistGridFSFile($class->name);
16021618
}
16031619

1620+
if ($class->isView()) {
1621+
return;
1622+
}
1623+
16041624
$this->persistNew($class, $document);
16051625
break;
16061626

@@ -1666,7 +1686,7 @@ private function doRemove(object $document, array &$visited) : void
16661686
break;
16671687
case self::STATE_MANAGED:
16681688
$this->lifecycleEventManager->preRemove($class, $document);
1669-
$this->scheduleForDelete($document);
1689+
$this->scheduleForDelete($document, $class->isView());
16701690
break;
16711691
case self::STATE_DETACHED:
16721692
throw MongoDBException::detachedDocumentCannotBeRemoved();
@@ -2560,7 +2580,7 @@ public function getOrCreateDocument(string $className, array $data, array &$hint
25602580
$isManagedObject = false;
25612581
$serializedId = null;
25622582
$id = null;
2563-
if (! $class->isQueryResultDocument && ! $class->isView()) {
2583+
if (! $class->isQueryResultDocument) {
25642584
$id = $class->getDatabaseIdentifierValue($data['_id']);
25652585
$serializedId = serialize($id);
25662586
$isManagedObject = isset($this->identityMap[$class->name][$serializedId]);
@@ -2588,7 +2608,7 @@ public function getOrCreateDocument(string $className, array $data, array &$hint
25882608
$document = $class->newInstance();
25892609
}
25902610

2591-
if (! $class->isQueryResultDocument && ! $class->isView()) {
2611+
if (! $class->isQueryResultDocument) {
25922612
$this->registerManaged($document, $id, $data);
25932613
$oid = spl_object_hash($document);
25942614
$this->documentStates[$oid] = self::STATE_MANAGED;

lib/Doctrine/ODM/MongoDB/Utility/LifecycleEventManager.php

+17-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Doctrine\ODM\MongoDB\Utility;
66

7+
use Doctrine\Common\EventArgs;
78
use Doctrine\Common\EventManager;
89
use Doctrine\ODM\MongoDB\DocumentManager;
910
use Doctrine\ODM\MongoDB\Event\DocumentNotFoundEventArgs;
@@ -65,7 +66,7 @@ public function postCollectionLoad(PersistentCollectionInterface $coll) : void
6566
public function postPersist(ClassMetadata $class, object $document) : void
6667
{
6768
$class->invokeLifecycleCallbacks(Events::postPersist, $document, [new LifecycleEventArgs($document, $this->dm)]);
68-
$this->evm->dispatchEvent(Events::postPersist, new LifecycleEventArgs($document, $this->dm));
69+
$this->dispatchEvent($class, Events::postPersist, new LifecycleEventArgs($document, $this->dm));
6970
$this->cascadePostPersist($class, $document);
7071
}
7172

@@ -75,7 +76,7 @@ public function postPersist(ClassMetadata $class, object $document) : void
7576
public function postRemove(ClassMetadata $class, object $document) : void
7677
{
7778
$class->invokeLifecycleCallbacks(Events::postRemove, $document, [new LifecycleEventArgs($document, $this->dm)]);
78-
$this->evm->dispatchEvent(Events::postRemove, new LifecycleEventArgs($document, $this->dm));
79+
$this->dispatchEvent($class, Events::postRemove, new LifecycleEventArgs($document, $this->dm));
7980
}
8081

8182
/**
@@ -85,7 +86,7 @@ public function postRemove(ClassMetadata $class, object $document) : void
8586
public function postUpdate(ClassMetadata $class, object $document) : void
8687
{
8788
$class->invokeLifecycleCallbacks(Events::postUpdate, $document, [new LifecycleEventArgs($document, $this->dm)]);
88-
$this->evm->dispatchEvent(Events::postUpdate, new LifecycleEventArgs($document, $this->dm));
89+
$this->dispatchEvent($class, Events::postUpdate, new LifecycleEventArgs($document, $this->dm));
8990
$this->cascadePostUpdate($class, $document);
9091
}
9192

@@ -95,7 +96,7 @@ public function postUpdate(ClassMetadata $class, object $document) : void
9596
public function prePersist(ClassMetadata $class, object $document) : void
9697
{
9798
$class->invokeLifecycleCallbacks(Events::prePersist, $document, [new LifecycleEventArgs($document, $this->dm)]);
98-
$this->evm->dispatchEvent(Events::prePersist, new LifecycleEventArgs($document, $this->dm));
99+
$this->dispatchEvent($class, Events::prePersist, new LifecycleEventArgs($document, $this->dm));
99100
}
100101

101102
/**
@@ -104,7 +105,7 @@ public function prePersist(ClassMetadata $class, object $document) : void
104105
public function preRemove(ClassMetadata $class, object $document) : void
105106
{
106107
$class->invokeLifecycleCallbacks(Events::preRemove, $document, [new LifecycleEventArgs($document, $this->dm)]);
107-
$this->evm->dispatchEvent(Events::preRemove, new LifecycleEventArgs($document, $this->dm));
108+
$this->dispatchEvent($class, Events::preRemove, new LifecycleEventArgs($document, $this->dm));
108109
}
109110

110111
/**
@@ -116,7 +117,7 @@ public function preUpdate(ClassMetadata $class, object $document) : void
116117
$class->invokeLifecycleCallbacks(Events::preUpdate, $document, [new PreUpdateEventArgs($document, $this->dm, $this->uow->getDocumentChangeSet($document))]);
117118
$this->uow->recomputeSingleDocumentChangeSet($class, $document);
118119
}
119-
$this->evm->dispatchEvent(Events::preUpdate, new PreUpdateEventArgs($document, $this->dm, $this->uow->getDocumentChangeSet($document)));
120+
$this->dispatchEvent($class, Events::preUpdate, new PreUpdateEventArgs($document, $this->dm, $this->uow->getDocumentChangeSet($document)));
120121
$this->cascadePreUpdate($class, $document);
121122
}
122123

@@ -160,7 +161,7 @@ private function cascadePostUpdate(ClassMetadata $class, object $document) : voi
160161
$entryClass = $this->dm->getClassMetadata(get_class($entry));
161162
$event = $this->uow->isScheduledForInsert($entry) ? Events::postPersist : Events::postUpdate;
162163
$entryClass->invokeLifecycleCallbacks($event, $entry, [new LifecycleEventArgs($entry, $this->dm)]);
163-
$this->evm->dispatchEvent($event, new LifecycleEventArgs($entry, $this->dm));
164+
$this->dispatchEvent($entryClass, $event, new LifecycleEventArgs($entry, $this->dm));
164165

165166
$this->cascadePostUpdate($entryClass, $entry);
166167
}
@@ -183,4 +184,13 @@ private function cascadePostPersist(ClassMetadata $class, object $document) : vo
183184
}
184185
}
185186
}
187+
188+
private function dispatchEvent(ClassMetadata $class, string $eventName, ?EventArgs $eventArgs = null)
189+
{
190+
if ($class->isView()) {
191+
return;
192+
}
193+
194+
$this->evm->dispatchEvent($eventName, $eventArgs);
195+
}
186196
}

0 commit comments

Comments
 (0)