Skip to content

Commit

Permalink
remove duplicate original data tracking (#PHPCR-57)
Browse files Browse the repository at this point in the history
  • Loading branch information
lsmith77 committed Mar 4, 2012
1 parent 0dc678f commit 7280b41
Showing 1 changed file with 37 additions and 62 deletions.
99 changes: 37 additions & 62 deletions lib/Doctrine/ODM/PHPCR/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,6 @@ class UnitOfWork
*/
private $documentLocales = array();

/**
* PHPCR always returns and updates the whole data of a document. If on update data is "missing"
* this means the data is deleted. This also applies to attachments. This is why we need to ensure
* that data that is not mapped is not lost. This map here saves all the "left-over" data and keeps
* track of it if necessary.
*
* @var array
*/
private $nonMappedData = array();

/**
* @var array
*/
private $originalData = array();

/**
* @var array
*/
Expand Down Expand Up @@ -238,7 +223,6 @@ public function createDocument($className, $node, array &$hints = array())
$class = $this->dm->getClassMetadata($className);

$documentState = array();
$nonMappedData = array();
$id = $node->getPath();

// second param is false to get uuid rather than dereference reference properties to node instances
Expand Down Expand Up @@ -344,11 +328,9 @@ public function createDocument($className, $node, array &$hints = array())
}

if ($overrideLocalValuesOid) {
$this->nonMappedData[$overrideLocalValuesOid] = $nonMappedData;
foreach ($class->reflFields as $prop => $reflFields) {
$value = isset($documentState[$prop]) ? $documentState[$prop] : null;
$reflFields->setValue($document, $value);
$this->originalData[$overrideLocalValuesOid][$prop] = $value;
}
}

Expand Down Expand Up @@ -782,35 +764,33 @@ private function computeChangeSet(ClassMetadata $class, $document)
unset($actualData[$class->versionCreatedField]);
}

if ($class->parentMapping && isset($actualData[$class->parentMapping])) {
$parentClass = $this->dm->getClassMetadata(get_class($actualData[$class->parentMapping]));
}

$id = $this->getDocumentId($document);
$oid = spl_object_hash($document);
if (!isset($this->originalData[$oid])) {
// Document is New and should be inserted
$this->originalData[$oid] = $actualData;
if (!$this->session->nodeExists($id)) {
// Document is new and should be inserted
$this->documentChangesets[$oid] = $actualData;
$this->scheduledInserts[$oid] = $document;
} else {
if (isset($this->originalData[$oid][$class->nodename])
&& isset($actualData[$class->nodename])
&& $this->originalData[$oid][$class->nodename] !== $actualData[$class->nodename]
$node = $this->session->getNode($id);
$originalData = $node->getPropertiesValues();
if (isset($actualData[$class->nodename])
&& $node->getName() !== $actualData[$class->nodename]
) {
throw new PHPCRException('The Nodename property is immutable ('.$this->originalData[$oid][$class->nodename].' !== '.$actualData[$class->nodename].'). Please use DocumentManager::move to rename the document: '.self::objToStr($document, $this->dm));
throw new PHPCRException('The Nodename property is immutable ('.$node->getName().' !== '.$actualData[$class->nodename].'). Please use DocumentManager::move to rename the document: '.self::objToStr($document, $this->dm));
}
if (isset($this->originalData[$oid][$class->parentMapping])
&& isset($actualData[$class->parentMapping])
&& $this->originalData[$oid][$class->parentMapping] !== $actualData[$class->parentMapping]
) {
throw new PHPCRException('The ParentDocument property is immutable ('.$class->getIdentifierValue($this->originalData[$oid][$class->parentMapping]).' !== '.$class->getIdentifierValue($actualData[$class->parentMapping]).'). Please use PHPCR\Session::move to move the document: '.self::objToStr($document, $this->dm));
if (isset($parentClass) && ($parent = $this->getDocumentById(dirname($id))) && $parent !== $actualData[$class->parentMapping]) {
throw new PHPCRException('The ParentDocument property is immutable ('.self::objToStr($parent, $this->dm).' !== '.self::objToStr($actualData[$class->parentMapping], $this->dm).'). Please use PHPCR\Session::move to move the document: '.self::objToStr($document, $this->dm));
}
if (isset($this->originalData[$oid][$class->identifier])
&& isset($actualData[$class->identifier])
&& $this->originalData[$oid][$class->identifier] !== $actualData[$class->identifier]
if (isset($actualData[$class->identifier])
&& $id !== $actualData[$class->identifier]
) {
throw new PHPCRException('The Id is immutable ('.$this->originalData[$oid][$class->identifier].' !== '.$actualData[$class->identifier].'). Please use DocumentManager::move to move the document: '.self::objToStr($document, $this->dm));
throw new PHPCRException('The Id is immutable ('.$id.' !== '.$actualData[$class->identifier].'). Please use DocumentManager::move to move the document: '.self::objToStr($document, $this->dm));
}

// Document is "fully" MANAGED: it was already fully persisted before
// and we have a copy of the original data

$changed = false;
foreach ($actualData as $fieldName => $fieldValue) {
if (!isset($class->fieldMappings[$fieldName])
Expand All @@ -832,7 +812,9 @@ private function computeChangeSet(ClassMetadata $class, $document)
$changed = true;
break;
}
} elseif ($this->originalData[$oid][$fieldName] !== $fieldValue) {
} elseif (!array_key_exists($fieldName, $originalData)
|| $originalData[$fieldName] !== $fieldValue
) {
$changed = true;
break;
} elseif ($fieldValue instanceof ReferenceManyCollection) {
Expand All @@ -854,20 +836,18 @@ private function computeChangeSet(ClassMetadata $class, $document)
}
}

if ($class->parentMapping && isset($actualData[$class->parentMapping])) {
$parent = $actualData[$class->parentMapping];
$parentClass = $this->dm->getClassMetadata(get_class($parent));
$state = $this->getDocumentState($parent);

if (isset($parentClass)) {
$state = $this->getDocumentState($actualData[$class->parentMapping]);
if ($state === self::STATE_MANAGED) {
$this->computeChangeSet($parentClass, $parent);
$this->computeChangeSet($parentClass, $actualData[$class->parentMapping]);
}
}

$id = $class->getIdentifierValue($document);
foreach ($class->childMappings as $name => $childMapping) {
if ($actualData[$name]) {
if ($this->originalData[$oid][$name] && $this->originalData[$oid][$name] !== $actualData[$name]) {
$child = $this->getDocumentById($id.'/'.$childMapping['name']);
if ($child && $child !== $actualData[$name]) {
throw new PHPCRException('Cannot move/copy children by assignment as it would be ambiguous. Please use the DocumentManager::move() or PHPCR\Session::copy() operations for this: '.self::objToStr($document, $this->dm));
}
$this->computeChildChanges($childMapping, $actualData[$name], $id);
Expand All @@ -889,8 +869,8 @@ private function computeChangeSet(ClassMetadata $class, $document)
}

foreach ($class->referrersMappings as $name => $referrerMapping) {
if ($this->originalData[$oid][$name]) {
foreach ($this->originalData[$oid][$name] as $referrer) {
if (isset($originalData[$name])) {
foreach ($originalData[$name] as $referrer) {
$this->computeReferrerChanges($referrer);
}
}
Expand Down Expand Up @@ -1333,16 +1313,16 @@ private function executeUpdates($documents, $dispatchEvents = true)
$node->setProperty($class->associationsMappings[$fieldName]['fieldName'], $associatedNode->getIdentifier(), $type);
}
}
} elseif (isset($class->childMappings[$fieldName])) {
if ($fieldValue === null) {
if ($node->hasNode($class->childMappings[$fieldName]['name'])) {
$child = $node->getNode($class->childMappings[$fieldName]['name']);
$childDocument = $this->createDocument(null, $child);
$this->purgeChildren($childDocument);
$child->remove();
}
} elseif ($this->originalData[$oid][$fieldName] && $this->originalData[$oid][$fieldName] !== $fieldValue) {
throw new PHPCRException('Cannot move/copy children by assignment as it would be ambiguous. Please use the DocumentManager::move() or PHPCR\Session::copy() operations for this.');
} elseif (isset($class->childMappings[$fieldName])
&& $node->hasNode($class->childMappings[$fieldName]['name'])
&& $fieldValue === null
) {
$childDocument = $this->getDocumentById($node->getPath().'/'.$class->childMappings[$fieldName]['name']);
if (empty($childDocument) || $childDocument instanceof Proxy) {
$child = $node->getNode($class->childMappings[$fieldName]['name']);
$childDocument = $this->createDocument(null, $child);
$this->purgeChildren($childDocument);
$child->remove();
}
}
}
Expand Down Expand Up @@ -1404,7 +1384,6 @@ private function executeMoves($documents)
$class = $this->dm->getClassMetadata(get_class($document));
if ($class->identifier) {
$class->setIdentifierValue($document, $newId);
$this->originalData[$oid][$class->identifier] = $newId;
}
}
}
Expand Down Expand Up @@ -1613,12 +1592,10 @@ private function unregisterDocument($document)
$this->scheduledMoves[$oid],
$this->scheduledInserts[$oid],
$this->scheduledAssociationUpdates[$oid],
$this->originalData[$oid],
$this->documentIds[$oid],
$this->documentState[$oid],
$this->documentTranslations[$oid],
$this->documentLocales[$oid],
$this->nonMappedData[$oid],
$this->documentChangesets[$oid],
$this->documentHistory[$oid],
$this->documentVersion[$oid]
Expand Down Expand Up @@ -1777,8 +1754,6 @@ public function clear()
$this->documentState =
$this->documentTranslations =
$this->documentLocales =
$this->nonMappedData =
$this->originalData =
$this->documentChangesets =
$this->scheduledUpdates =
$this->scheduledAssociationUpdates =
Expand Down

0 comments on commit 7280b41

Please sign in to comment.