Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

fixed handling of transactions, CS fixes, removed unused code

  • Loading branch information...
commit 688fda8e2bddb3b9342d00537aab8dc38b785a9f 1 parent b554c05
Lukas Kahwe Smith lsmith77 authored

Showing 1 changed file with 39 additions and 49 deletions. Show diff stats Hide diff stats

  1. +39 49 lib/Doctrine/ODM/PHPCR/UnitOfWork.php
88 lib/Doctrine/ODM/PHPCR/UnitOfWork.php
@@ -204,7 +204,7 @@ public function createDocument($documentName, $node, array &$hints = array())
204 204 foreach ($class->fieldMappings as $fieldName => $mapping) {
205 205 if (isset($properties[$mapping['name']])) {
206 206 if ($mapping['multivalue']) {
207   - $documentState[$fieldName] = new ArrayCollection($properties[$mapping['name']]);
  207 + $documentState[$fieldName] = new ArrayCollection((array)$properties[$mapping['name']]);
208 208 } else {
209 209 $documentState[$fieldName] = $properties[$mapping['name']];
210 210 }
@@ -224,8 +224,8 @@ public function createDocument($documentName, $node, array &$hints = array())
224 224 // collect uuids of all referenced nodes and get them all within one single call
225 225 // they will get cached so you have more performance when they are accessed later
226 226 $refNodeUUIDs = array();
227   - foreach ($class->associationsMappings as $assocName => $assocOptions) {
228   - if (! $node->hasProperty($assocOptions['fieldName'])) {
  227 + foreach ($class->associationsMappings as $assocOptions) {
  228 + if (!$node->hasProperty($assocOptions['fieldName'])) {
229 229 continue;
230 230 }
231 231
@@ -238,11 +238,6 @@ public function createDocument($documentName, $node, array &$hints = array())
238 238 }
239 239 }
240 240
241   - if (count($refNodeUUIDs) > 0) {
242   - $session = $this->dm->getPhpcrSession();
243   - $refNodes = $session->getNodesByIdentifier($refNodeUUIDs);
244   - }
245   -
246 241 // initialize inverse side collections
247 242 foreach ($class->associationsMappings as $assocName => $assocOptions) {
248 243 if ($assocOptions['type'] & ClassMetadata::MANY_TO_ONE) {
@@ -531,7 +526,7 @@ public function getDocumentState($document)
531 526
532 527 private function detectChangedDocuments()
533 528 {
534   - foreach ($this->identityMap as $id => $document) {
  529 + foreach ($this->identityMap as $document) {
535 530 $state = $this->getDocumentState($document);
536 531 if ($state == self::STATE_MANAGED) {
537 532 $class = $this->dm->getClassMetadata(get_class($document));
@@ -653,7 +648,7 @@ private function computeChildChanges($mapping, $child, $parentId)
653 648 {
654 649 $targetClass = $this->dm->getClassMetadata(get_class($child));
655 650 $state = $this->getDocumentState($child);
656   - $oid = spl_object_hash($child);
  651 +
657 652 if ($state == self::STATE_NEW) {
658 653 $targetClass->reflFields[$targetClass->identifier]->setValue($child , $parentId . '/'. $mapping['name']);
659 654 $this->persistNew($targetClass, $child, ClassMetadata::GENERATOR_TYPE_ASSIGNED);
@@ -675,7 +670,7 @@ private function computeReferenceChanges($mapping, $reference, $referrerId)
675 670 {
676 671 $targetClass = $this->dm->getClassMetadata(get_class($reference));
677 672 $state = $this->getDocumentState($reference);
678   - $oid = spl_object_hash($reference);
  673 +
679 674 if ($state == self::STATE_NEW) {
680 675 $this->persistNew($targetClass, $reference, ClassMetadata::GENERATOR_TYPE_ASSIGNED);
681 676 $this->computeChangeSet($targetClass, $reference);
@@ -694,14 +689,16 @@ private function computeReferrerChanges($mapping, $referrer, $referenceId)
694 689 {
695 690 $targetClass = $this->dm->getClassMetadata(get_class($referrer));
696 691 $state = $this->getDocumentState($referrer);
697   - $oid = spl_object_hash($referrer);
698   - if ($state == self::STATE_NEW) {
699   - $this->persistNew($targetClass, $referrer, ClassMetadata::GENERATOR_TYPE_ASSIGNED);
700   - $this->computeChangeSet($targetClass, $referrer);
701   - } else if ($state == self::STATE_DETACHED) {
702   - // TODO: can this actually happen?
703   - throw new \InvalidArgumentException("A detached document was found through a "
704   - . "referrer during cascading a persist operation.");
  692 +
  693 + switch ($state) {
  694 + case self::STATE_NEW:
  695 + $this->persistNew($targetClass, $referrer, ClassMetadata::GENERATOR_TYPE_ASSIGNED);
  696 + $this->computeChangeSet($targetClass, $referrer);
  697 + break;
  698 + case self::STATE_DETACHED:
  699 + // TODO: can this actually happen?
  700 + throw new \InvalidArgumentException("A detached document was found through a "
  701 + . "referrer during cascading a persist operation.");
705 702 }
706 703 }
707 704
@@ -769,7 +766,9 @@ public function flush($persist_to_backend = true)
769 766
770 767 if ($persist_to_backend) {
771 768 $utx = $session->getWorkspace()->getTransactionManager();
772   - $utx->begin();
  769 + if ($utx) {
  770 + $utx->begin();
  771 + }
773 772 }
774 773
775 774 foreach ($this->scheduledRemovals as $oid => $document) {
@@ -806,7 +805,7 @@ public function flush($persist_to_backend = true)
806 805
807 806 if ($class->referenceable) {
808 807 $node->addMixin("mix:referenceable");
809   - // TODO make shure uuid is unique
  808 + // TODO make sure uuid is unique
810 809 $node->setProperty("jcr:uuid", \PHPCR\Util\UUIDHelper::generateUUID());
811 810 }
812 811
@@ -825,7 +824,7 @@ public function flush($persist_to_backend = true)
825 824 if (isset($class->fieldMappings[$fieldName])) {
826 825 $type = \PHPCR\PropertyType::valueFromName($class->fieldMappings[$fieldName]['type']);
827 826 if ($class->fieldMappings[$fieldName]['multivalue']) {
828   - $value = $fieldValue === null ? null : $fieldValue->toArray();
  827 + $value = $fieldValue ?: $fieldValue->toArray();
829 828 $node->setProperty($class->fieldMappings[$fieldName]['name'], $value, $type);
830 829 } else {
831 830 $node->setProperty($class->fieldMappings[$fieldName]['name'], $fieldValue, $type);
@@ -857,7 +856,7 @@ public function flush($persist_to_backend = true)
857 856 if (isset($class->fieldMappings[$fieldName])) {
858 857 $type = \PHPCR\PropertyType::valueFromName($class->fieldMappings[$fieldName]['type']);
859 858 if ($class->fieldMappings[$fieldName]['multivalue']) {
860   - $value = $fieldValue === null ? null : $fieldValue->toArray();
  859 + $value = $fieldValue ?: $fieldValue->toArray();
861 860 $node->setProperty($class->fieldMappings[$fieldName]['name'], $value, $type);
862 861 } else {
863 862 $node->setProperty($class->fieldMappings[$fieldName]['name'], $fieldValue, $type);
@@ -869,11 +868,8 @@ public function flush($persist_to_backend = true)
869 868 continue;
870 869 }
871 870
872   - if($class->associationsMappings[$fieldName]['weak']) {
873   - $type = \PHPCR\PropertyType::WEAKREFERENCE;
874   - } else {
875   - $type = \PHPCR\PropertyType::REFERENCE;
876   - }
  871 + $type = $class->associationsMappings[$fieldName]['weak']
  872 + ? \PHPCR\PropertyType::WEAKREFERENCE : \PHPCR\PropertyType::REFERENCE;
877 873
878 874 if ($class->associationsMappings[$fieldName]['type'] === $class::MANY_TO_MANY) {
879 875 if (isset($fieldValue)) {
@@ -909,35 +905,29 @@ public function flush($persist_to_backend = true)
909 905 } elseif (is_null($this->originalData[$oid][$fieldName])) {
910 906 // TODO: store this new child
911 907 } elseif (isset($this->originalData[$oid][$fieldName])) {
912   - //TODO: is this the correct test? if you put a different document as child and already had one, it means you moved stuff?
913   - if ($fieldValue === $this->originalData[$oid][$fieldName]) {
914   - //TODO: save
915   - } else {
916   - // this is currently not implemented
917   - // the old child needs to be removed and the new child might be moved
918   - 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.");
919   - }
  908 + //TODO: is this the correct test? if you put a different document as child and already had one, it means you moved stuff?
  909 + if ($fieldValue === $this->originalData[$oid][$fieldName]) {
  910 + //TODO: save
  911 + } else {
  912 + // this is currently not implemented
  913 + // the old child needs to be removed and the new child might be moved
  914 + 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.");
  915 + }
920 916 }
921 917 }
922 918 }
923   -
924   - // respect the non mapped data, otherwise they will be deleted.
925   - if (isset($this->nonMappedData[$oid]) && $this->nonMappedData[$oid]) {
926   - $data = array_merge($data, $this->nonMappedData[$oid]);
927   - }
928   -
929   - $rev = $this->getDocumentRevision($document);
930   - if ($rev) {
931   - $data['_rev'] = $rev;
932   - }
933 919 }
934 920
935 921 if ($persist_to_backend) {
936 922 try {
937 923 $session->save();
938   - $utx->commit();
939   - } catch (Exception $e) {
940   - $utx->rollback();
  924 + if ($utx) {
  925 + $utx->commit();
  926 + }
  927 + } catch (\Exception $e) {
  928 + if ($utx) {
  929 + $utx->rollback();
  930 + }
941 931 }
942 932 }
943 933

0 comments on commit 688fda8

Please sign in to comment.
Something went wrong with that request. Please try again.