/** * Executes a merge operation on an document. * * @param object $document * @param array $visited * @return object The managed copy of the document. * @throws InvalidArgumentException If the document instance is NEW. */ private function doMerge($document, array &$visited, $prevManagedCopy = null, $assoc = null) { $oid = spl_object_hash($document); if (isset($visited[$oid])) { return; // Prevent infinite recursion } $visited[$oid] = $document; // mark visited $class = $this->dm->getClassMetadata(get_class($document)); // First we assume DETACHED, although it can still be NEW but we can avoid // an extra db-roundtrip this way. If it is not MANAGED but has an identity, // we need to fetch it from the db anyway in order to merge. // MANAGED documents are ignored by the merge operation. if ($this->getDocumentState($document, self::STATE_DETACHED) == self::STATE_MANAGED) { $managedCopy = $document; } else { // Try to look the entity up in the identity map. $id = $class->getIdentifierValue($document); // If there is no ID, it is actually NEW. if (!$id) { $managedCopy = $class->newInstance(); $this->persistNew($class, $managedCopy); } else { $managedCopy = $this->tryGetById($id, $class->rootDocumentName); if ($managedCopy) { // We have the entity in-memory already, just make sure its not removed. if ($this->getDocumentState($managedCopy) == self::STATE_REMOVED) { throw new InvalidArgumentException('Removed entity detected during merge.' . ' Can not merge with a removed entity.'); } } else { // We need to fetch the managed copy in order to merge. $managedCopy = $this->dm->find($class->name, $id); } if ($managedCopy === null) { // If the identifier is ASSIGNED, it is NEW, otherwise an error // since the managed entity was not found. $managedCopy = $class->newInstance(); $class->setIdentifierValue($managedCopy, $id); $this->persistNew($class, $managedCopy); } } if ($class->isVersioned) { $managedCopyVersion = $class->reflFields[$class->versionField]->getValue($managedCopy); $documentVersion = $class->reflFields[$class->versionField]->getValue($document); // Throw exception if versions dont match. if ($managedCopyVersion != $documentVersion) { throw LockException::lockFailedVersionMissmatch($documentVersion, $managedCopyVersion); } } // Merge state of $document into existing (managed) entity foreach ($class->reflFields as $name => $prop) { if (!isset($class->fieldMappings[$name]['embedded']) && !isset($class->fieldMappings[$name]['reference'])) { $prop->setValue($managedCopy, $prop->getValue($document)); } else { $assoc2 = $class->fieldMappings[$name]; if ($assoc2['type'] === 'one') { $other = $prop->getValue($document); if ($other === null) { $prop->setValue($managedCopy, null); } else { if ($other instanceof Proxy && !$other->__isInitialized__) { // do not merge fields marked lazy that have not been fetched. continue; } else { if (!isset($assoc2['embedded']) && !$assoc2['isCascadeMerge']) { if ($this->getDocumentState($other, self::STATE_DETACHED) == self::STATE_MANAGED) { $prop->setValue($managedCopy, $other); } else { $targetDocument = isset($assoc2['targetDocument']) ? $assoc2['targetDocument'] : get_class($other); $targetClass = $this->dm->getClassMetadata($targetDocument); $id = $targetClass->getIdentifierValue($other); $proxy = $this->dm->getProxyFactory()->getProxy($targetDocument, $id); $prop->setValue($managedCopy, $proxy); $this->registerManaged($proxy, $id, array()); } } } } } else { $mergeCol = $prop->getValue($document); if ($mergeCol instanceof PersistentCollection && !$mergeCol->isInitialized()) { // do not merge fields marked lazy that have not been fetched. // keep the lazy persistent collection of the managed copy. continue; } foreach ($mergeCol as $entry) { $targetDocument = isset($assoc2['targetDocument']) ? $assoc2['targetDocument'] : get_class($entry); $targetClass = $this->dm->getClassMetadata($targetDocument); if ($targetClass->isEmbeddedDocument) { $this->registerManaged($entry, null, array()); } else { $id = $targetClass->getIdentifierValue($entry); $this->registerManaged($entry, $id, array()); } } if (!$mergeCol instanceof PersistentCollection) { $mergeCol = new PersistentCollection($mergeCol, $this->dm, $this, $this->cmd); $mergeCol->setInitialized(true); } $mergeCol->setOwner($managedCopy, $assoc2); $prop->setValue($managedCopy, $mergeCol); } } if ($class->isChangeTrackingNotify()) { // Just treat all properties as changed, there is no other choice. $this->propertyChanged($managedCopy, $name, null, $prop->getValue($managedCopy)); } } if ($class->isChangeTrackingDeferredExplicit()) { $this->scheduleForDirtyCheck($document); } } if ($prevManagedCopy !== null) { $assocField = $assoc->sourceFieldName; $prevClass = $this->dm->getClassMetadata(get_class($prevManagedCopy)); if ($assoc->isOneToOne()) { $prevClass->reflFields[$assocField]->setValue($prevManagedCopy, $managedCopy); } else { $prevClass->reflFields[$assocField]->getValue($prevManagedCopy)->unwrap()->add($managedCopy); if ($assoc->isOneToMany()) { $class->reflFields[$assoc->mappedBy]->setValue($managedCopy, $prevManagedCopy); } } } // Mark the managed copy visited as well $visited[spl_object_hash($managedCopy)] = true; $this->cascadeMerge($document, $managedCopy, $visited); return $managedCopy; }