Permalink
Browse files

Merge pull request #58 from doctrine/cascade_fixes

fixed several issues in cascade support
  • Loading branch information...
2 parents 6e8dbe8 + add3bae commit 23f95b729edffe62430802c03da14131c2e483e6 @beberlei beberlei committed Feb 24, 2013
@@ -94,23 +94,13 @@ public function loadMetadataForClass($className, ClassMetadata $class)
} else if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\Index) {
$mapping['indexed'] = true;
} else if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\ReferenceOne) {
- $cascade = 0;
- foreach ($fieldAnnot->cascade AS $cascadeMode) {
- $cascade += constant('Doctrine\ODM\CouchDB\Mapping\ClassMetadata::CASCADE_' . strtoupper($cascadeMode));
- }
- $fieldAnnot->cascade = $cascade;
-
$mapping = array_merge($mapping, (array) $fieldAnnot);
+ $mapping['cascade'] = $this->getCascadeMode($fieldAnnot->cascade);
unset($mapping['value']);
$class->mapManyToOne($mapping);
} else if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\ReferenceMany) {
- $cascade = 0;
- foreach ($fieldAnnot->cascade AS $cascadeMode) {
- $cascade += constant('Doctrine\ODM\CouchDB\Mapping\ClassMetadata::CASCADE_' . strtoupper($cascadeMode));
- }
- $fieldAnnot->cascade = $cascade;
-
$mapping = array_merge($mapping, (array) $fieldAnnot);
+ $mapping['cascade'] = $this->getCascadeMode($fieldAnnot->cascade);
unset($mapping['value']);
$class->mapManyToMany($mapping);
} else if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\Attachments) {
@@ -127,4 +117,23 @@ public function loadMetadataForClass($className, ClassMetadata $class)
}
}
}
+
+ /**
+ * Gathers a list of cascade options found in the given cascade element.
+ *
+ * @param $cascadeList cascade list
+ * @return integer a bitmask of cascade options.
+ */
+ private function getCascadeMode(array $cascadeList)
+ {
+ $cascade = 0;
+ foreach ($cascadeList as $cascadeMode) {
+ $constantName = 'Doctrine\ODM\CouchDB\Mapping\ClassMetadata::CASCADE_' . strtoupper($cascadeMode);
+ if (!defined($constantName)) {
+ throw new MappingException("Cascade mode '$cascadeMode' not supported.");
+ }
+ $cascade |= constant($constantName);
+ }
+ return $cascade;
+ }
}
@@ -189,10 +189,10 @@ protected function loadMappingFile($file)
/**
* Gathers a list of cascade options found in the given cascade element.
*
- * @param $cascadeElement The cascade element.
- * @return array The list of cascade options.
+ * @param $cascadeElement cascade element.
+ * @return integer a bitmask of cascade options.
*/
- private function getCascadeMode($cascadeElement)
+ private function getCascadeMode(SimpleXMLElement $cascadeElement)
{
$cascade = 0;
foreach ($cascadeElement->children() as $action) {
@@ -202,8 +202,13 @@ private function getCascadeMode($cascadeElement)
// and we want to make sure that this driver doesn't need to know
// anything about the supported cascading actions
$cascadeMode = str_replace('cascade-', '', $action->getName());
- $cascade = constant('Doctrine\ODM\CouchDB\Mapping\ClassMetadata::CASCADE_' . strtoupper($cascadeMode));
+ $constantName = 'Doctrine\ODM\CouchDB\Mapping\ClassMetadata::CASCADE_' . strtoupper($cascadeMode);
+ if (!defined($constantName)) {
+ throw new MappingException("Cascade mode '$cascadeMode' not supported.");
+ }
+ $cascade |= constant($constantName);
}
+
return $cascade;
}
}
@@ -115,7 +115,7 @@ public function loadMetadataForClass($className, ClassMetadata $class)
if (isset($element['referenceOne'])) {
foreach ($element['referenceOne'] AS $field => $referenceOneElement) {
$class->mapManyToOne(array(
- 'cascade' => (isset($referenceManyElement->cascade)) ? $this->getCascadeMode($referenceManyElement->cascade) : 0,
+ 'cascade' => (isset($referenceOneElement['cascade'])) ? $this->getCascadeMode($referenceOneElement['cascade']) : 0,
'targetDocument' => (string)$referenceOneElement['targetDocument'],
'fieldName' => $field,
'jsonName' => (isset($referenceOneElement['jsonName'])) ? (string)$referenceOneElement['jsonName'] : null,
@@ -126,7 +126,7 @@ public function loadMetadataForClass($className, ClassMetadata $class)
if (isset($element['referenceMany'])) {
foreach ($element['referenceMany'] AS $field => $referenceManyElement) {
$class->mapManyToMany(array(
- 'cascade' => (isset($referenceManyElement->cascade)) ? $this->getCascadeMode($referenceManyElement->cascade) : 0,
+ 'cascade' => (isset($referenceManyElement['cascade'])) ? $this->getCascadeMode($referenceManyElement['cascade']) : 0,
'targetDocument' => (string)$referenceManyElement['targetDocument'],
'fieldName' => $field,
'jsonName' => (isset($referenceManyElement['jsonName'])) ? (string)$referenceManyElement['jsonName'] : null,
@@ -166,4 +166,24 @@ protected function loadMappingFile($file)
{
return Yaml::parse($file);
}
+
+ /**
+ * Gathers a list of cascade options found in the given cascade element.
+ *
+ * @param array $cascadeElement The cascade element.
+ * @return integer a bitmask of cascade options.
+ */
+ private function getCascadeMode(array $cascadeElement)
+ {
+ $cascade = 0;
+ foreach ($cascadeElement as $cascadeMode) {
+ $constantName = 'Doctrine\ODM\CouchDB\Mapping\ClassMetadata::CASCADE_' . strtoupper($cascadeMode);
+ if (!defined($constantName)) {
+ throw new MappingException("Cascade mode '$cascadeMode' not supported.");
+ }
+ $cascade |= constant($constantName);
+ }
+
+ return $cascade;
+ }
}

0 comments on commit 23f95b7

Please sign in to comment.