Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed handling of transactions, CS fixes, removed unused code

  • Loading branch information...
commit 688fda8e2bddb3b9342d00537aab8dc38b785a9f 1 parent b554c05
Lukas Kahwe Smith lsmith77 authored
Showing with 39 additions and 49 deletions.
  1. +39 −49 lib/Doctrine/ODM/PHPCR/UnitOfWork.php
88 lib/Doctrine/ODM/PHPCR/UnitOfWork.php
View
@@ -204,7 +204,7 @@ public function createDocument($documentName, $node, array &$hints = array())
foreach ($class->fieldMappings as $fieldName => $mapping) {
if (isset($properties[$mapping['name']])) {
if ($mapping['multivalue']) {
- $documentState[$fieldName] = new ArrayCollection($properties[$mapping['name']]);
+ $documentState[$fieldName] = new ArrayCollection((array)$properties[$mapping['name']]);
} else {
$documentState[$fieldName] = $properties[$mapping['name']];
}
@@ -224,8 +224,8 @@ public function createDocument($documentName, $node, array &$hints = array())
// collect uuids of all referenced nodes and get them all within one single call
// they will get cached so you have more performance when they are accessed later
$refNodeUUIDs = array();
- foreach ($class->associationsMappings as $assocName => $assocOptions) {
- if (! $node->hasProperty($assocOptions['fieldName'])) {
+ foreach ($class->associationsMappings as $assocOptions) {
+ if (!$node->hasProperty($assocOptions['fieldName'])) {
continue;
}
@@ -238,11 +238,6 @@ public function createDocument($documentName, $node, array &$hints = array())
}
}
- if (count($refNodeUUIDs) > 0) {
- $session = $this->dm->getPhpcrSession();
- $refNodes = $session->getNodesByIdentifier($refNodeUUIDs);
- }
-
// initialize inverse side collections
foreach ($class->associationsMappings as $assocName => $assocOptions) {
if ($assocOptions['type'] & ClassMetadata::MANY_TO_ONE) {
@@ -531,7 +526,7 @@ public function getDocumentState($document)
private function detectChangedDocuments()
{
- foreach ($this->identityMap as $id => $document) {
+ foreach ($this->identityMap as $document) {
$state = $this->getDocumentState($document);
if ($state == self::STATE_MANAGED) {
$class = $this->dm->getClassMetadata(get_class($document));
@@ -653,7 +648,7 @@ private function computeChildChanges($mapping, $child, $parentId)
{
$targetClass = $this->dm->getClassMetadata(get_class($child));
$state = $this->getDocumentState($child);
- $oid = spl_object_hash($child);
+
if ($state == self::STATE_NEW) {
$targetClass->reflFields[$targetClass->identifier]->setValue($child , $parentId . '/'. $mapping['name']);
$this->persistNew($targetClass, $child, ClassMetadata::GENERATOR_TYPE_ASSIGNED);
@@ -675,7 +670,7 @@ private function computeReferenceChanges($mapping, $reference, $referrerId)
{
$targetClass = $this->dm->getClassMetadata(get_class($reference));
$state = $this->getDocumentState($reference);
- $oid = spl_object_hash($reference);
+
if ($state == self::STATE_NEW) {
$this->persistNew($targetClass, $reference, ClassMetadata::GENERATOR_TYPE_ASSIGNED);
$this->computeChangeSet($targetClass, $reference);
@@ -694,14 +689,16 @@ private function computeReferrerChanges($mapping, $referrer, $referenceId)
{
$targetClass = $this->dm->getClassMetadata(get_class($referrer));
$state = $this->getDocumentState($referrer);
- $oid = spl_object_hash($referrer);
- if ($state == self::STATE_NEW) {
- $this->persistNew($targetClass, $referrer, ClassMetadata::GENERATOR_TYPE_ASSIGNED);
- $this->computeChangeSet($targetClass, $referrer);
- } else if ($state == self::STATE_DETACHED) {
- // TODO: can this actually happen?
- throw new \InvalidArgumentException("A detached document was found through a "
- . "referrer during cascading a persist operation.");
+
+ switch ($state) {
+ case self::STATE_NEW:
+ $this->persistNew($targetClass, $referrer, ClassMetadata::GENERATOR_TYPE_ASSIGNED);
+ $this->computeChangeSet($targetClass, $referrer);
+ break;
+ case self::STATE_DETACHED:
+ // TODO: can this actually happen?
+ throw new \InvalidArgumentException("A detached document was found through a "
+ . "referrer during cascading a persist operation.");
}
}
@@ -769,7 +766,9 @@ public function flush($persist_to_backend = true)
if ($persist_to_backend) {
$utx = $session->getWorkspace()->getTransactionManager();
- $utx->begin();
+ if ($utx) {
+ $utx->begin();
+ }
}
foreach ($this->scheduledRemovals as $oid => $document) {
@@ -806,7 +805,7 @@ public function flush($persist_to_backend = true)
if ($class->referenceable) {
$node->addMixin("mix:referenceable");
- // TODO make shure uuid is unique
+ // TODO make sure uuid is unique
$node->setProperty("jcr:uuid", \PHPCR\Util\UUIDHelper::generateUUID());
}
@@ -825,7 +824,7 @@ public function flush($persist_to_backend = true)
if (isset($class->fieldMappings[$fieldName])) {
$type = \PHPCR\PropertyType::valueFromName($class->fieldMappings[$fieldName]['type']);
if ($class->fieldMappings[$fieldName]['multivalue']) {
- $value = $fieldValue === null ? null : $fieldValue->toArray();
+ $value = $fieldValue ?: $fieldValue->toArray();
$node->setProperty($class->fieldMappings[$fieldName]['name'], $value, $type);
} else {
$node->setProperty($class->fieldMappings[$fieldName]['name'], $fieldValue, $type);
@@ -857,7 +856,7 @@ public function flush($persist_to_backend = true)
if (isset($class->fieldMappings[$fieldName])) {
$type = \PHPCR\PropertyType::valueFromName($class->fieldMappings[$fieldName]['type']);
if ($class->fieldMappings[$fieldName]['multivalue']) {
- $value = $fieldValue === null ? null : $fieldValue->toArray();
+ $value = $fieldValue ?: $fieldValue->toArray();
$node->setProperty($class->fieldMappings[$fieldName]['name'], $value, $type);
} else {
$node->setProperty($class->fieldMappings[$fieldName]['name'], $fieldValue, $type);
@@ -869,11 +868,8 @@ public function flush($persist_to_backend = true)
continue;
}
- if($class->associationsMappings[$fieldName]['weak']) {
- $type = \PHPCR\PropertyType::WEAKREFERENCE;
- } else {
- $type = \PHPCR\PropertyType::REFERENCE;
- }
+ $type = $class->associationsMappings[$fieldName]['weak']
+ ? \PHPCR\PropertyType::WEAKREFERENCE : \PHPCR\PropertyType::REFERENCE;
if ($class->associationsMappings[$fieldName]['type'] === $class::MANY_TO_MANY) {
if (isset($fieldValue)) {
@@ -909,35 +905,29 @@ public function flush($persist_to_backend = true)
} elseif (is_null($this->originalData[$oid][$fieldName])) {
// TODO: store this new child
} elseif (isset($this->originalData[$oid][$fieldName])) {
- //TODO: is this the correct test? if you put a different document as child and already had one, it means you moved stuff?
- if ($fieldValue === $this->originalData[$oid][$fieldName]) {
- //TODO: save
- } else {
- // this is currently not implemented
- // the old child needs to be removed and the new child might be moved
- throw new PHPCRException("You can not move or copy children by assignment as it would be ambiguous. Please use the PHPCR\Session::move() resp PHPCR\Session::copy() operations for this.");
- }
+ //TODO: is this the correct test? if you put a different document as child and already had one, it means you moved stuff?
+ if ($fieldValue === $this->originalData[$oid][$fieldName]) {
+ //TODO: save
+ } else {
+ // this is currently not implemented
+ // the old child needs to be removed and the new child might be moved
+ throw new PHPCRException("You can not move or copy children by assignment as it would be ambiguous. Please use the PHPCR\Session::move() resp PHPCR\Session::copy() operations for this.");
+ }
}
}
}
-
- // respect the non mapped data, otherwise they will be deleted.
- if (isset($this->nonMappedData[$oid]) && $this->nonMappedData[$oid]) {
- $data = array_merge($data, $this->nonMappedData[$oid]);
- }
-
- $rev = $this->getDocumentRevision($document);
- if ($rev) {
- $data['_rev'] = $rev;
- }
}
if ($persist_to_backend) {
try {
$session->save();
- $utx->commit();
- } catch (Exception $e) {
- $utx->rollback();
+ if ($utx) {
+ $utx->commit();
+ }
+ } catch (\Exception $e) {
+ if ($utx) {
+ $utx->rollback();
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.