Permalink
Browse files

support for attribute override

  • Loading branch information...
1 parent 4df3c75 commit 85790f0752faa9522a47134f1948a94d6b9819ec @FabioBatSilva FabioBatSilva committed Apr 14, 2012
View
@@ -146,6 +146,7 @@
<xs:element name="many-to-one" type="orm:many-to-one" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="many-to-many" type="orm:many-to-many" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="association-overrides" type="orm:association-overrides" minOccurs="0" maxOccurs="unbounded" />
+ <xs:element name="attribute-overrides" type="orm:attribute-overrides" minOccurs="0" maxOccurs="unbounded" />
<xs:any minOccurs="0" maxOccurs="unbounded" namespace="##other"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required" />
@@ -500,4 +501,19 @@
<xs:attribute name="name" type="xs:NMTOKEN" use="required" />
</xs:complexType>
+ <xs:complexType name="attribute-overrides">
+ <xs:sequence>
+ <xs:element name="attribute-override" type="orm:attribute-override" minOccurs="1" maxOccurs="unbounded" />
+ <xs:any minOccurs="1" maxOccurs="unbounded" namespace="##other"/>
+ </xs:sequence>
+ </xs:complexType>
+
+ <xs:complexType name="attribute-override">
+ <xs:sequence>
+ <xs:element name="field" type="orm:field" minOccurs="1" />
+ <xs:any minOccurs="1" maxOccurs="unbounded" namespace="##other"/>
+ </xs:sequence>
+ <xs:attribute name="name" type="xs:NMTOKEN" use="required" />
+ </xs:complexType>
+
</xs:schema>
@@ -1238,7 +1238,7 @@ protected function _validateAndCompleteAssociationMapping(array $mapping)
$mapping['isOwningSide'] = true; // assume owning side until we hit mappedBy
// unset optional indexBy attribute if its empty
- if (!isset($mapping['indexBy']) || !$mapping['indexBy']) {
+ if ( ! isset($mapping['indexBy']) || !$mapping['indexBy']) {
unset($mapping['indexBy']);
}
@@ -1364,7 +1364,7 @@ protected function _validateAndCompleteOneToOneMapping(array $mapping)
foreach ($mapping['joinColumns'] as $key => &$joinColumn) {
if ($mapping['type'] === self::ONE_TO_ONE && ! $this->isInheritanceTypeSingleTable()) {
if (count($mapping['joinColumns']) == 1) {
- if (! isset($mapping['id']) || ! $mapping['id']) {
+ if ( ! isset($mapping['id']) || ! $mapping['id']) {
$joinColumn['unique'] = true;
}
} else {
@@ -1383,7 +1383,7 @@ protected function _validateAndCompleteOneToOneMapping(array $mapping)
}
if ($uniqueContraintColumns) {
- if (!$this->table) {
+ if ( ! $this->table) {
throw new \RuntimeException("ClassMetadataInfo::setTable() has to be called before defining a one to one relationship.");
}
$this->table['uniqueConstraints'][$mapping['fieldName']."_uniq"] = array(
@@ -1810,7 +1810,7 @@ public function setInheritanceType($type)
*/
public function setAssociationOverride($fieldName, array $overrideMapping)
{
- if (!isset($this->associationMappings[$fieldName])) {
+ if ( ! isset($this->associationMappings[$fieldName])) {
throw MappingException::invalidOverrideFieldName($this->name, $fieldName);
}
@@ -1848,6 +1848,44 @@ public function setAssociationOverride($fieldName, array $overrideMapping)
$this->associationMappings[$fieldName] = $mapping;
}
+ /**
+ * Sets the override for a mapped field.
+ *
+ * @param string $fieldName
+ * @param array $mapping
+ */
+ public function setAttributeOverride($fieldName, array $overrideMapping)
+ {
+ if ( ! isset($this->fieldMappings[$fieldName])) {
+ throw MappingException::invalidOverrideFieldName($this->name, $fieldName);
+ }
+
+ $mapping = $this->fieldMappings[$fieldName];
+
+ if (isset($mapping['id'])) {
+ $overrideMapping['id'] = $mapping['id'];
+ }
+
+ if ( ! isset($overrideMapping['type']) || $overrideMapping['type'] === null) {
+ $overrideMapping['type'] = $mapping['type'];
+ }
+
+ if ( ! isset($overrideMapping['fieldName']) || $overrideMapping['fieldName'] === null) {
+ $overrideMapping['fieldName'] = $mapping['fieldName'];
+ }
+
+ if ($overrideMapping['type'] !== $mapping['type']) {
+ throw MappingException::invalidOverrideFieldType($this->name, $fieldName);
+ }
+
+ unset($this->fieldMappings[$fieldName]);
+ unset($this->fieldNames[$mapping['columnName']]);
+ unset($this->columnNames[$mapping['fieldName']]);
+ $this->_validateAndCompleteFieldMapping($overrideMapping);
+
+ $this->fieldMappings[$fieldName] = $overrideMapping;
+ }
+
/**
* Checks whether a mapped field is inherited from an entity superclass.
*
@@ -2408,7 +2446,7 @@ public function isAssociationWithSingleJoinColumn($fieldName)
*/
public function getSingleAssociationJoinColumnName($fieldName)
{
- if (!$this->isAssociationWithSingleJoinColumn($fieldName)) {
+ if ( ! $this->isAssociationWithSingleJoinColumn($fieldName)) {
throw MappingException::noSingleAssociationJoinColumnFound($this->name, $fieldName);
}
return $this->associationMappings[$fieldName]['joinColumns'][0]['name'];
@@ -2422,7 +2460,7 @@ public function getSingleAssociationJoinColumnName($fieldName)
*/
public function getSingleAssociationReferencedJoinColumnName($fieldName)
{
- if (!$this->isAssociationWithSingleJoinColumn($fieldName)) {
+ if ( ! $this->isAssociationWithSingleJoinColumn($fieldName)) {
throw MappingException::noSingleAssociationJoinColumnFound($this->name, $fieldName);
}
return $this->associationMappings[$fieldName]['joinColumns'][0]['referencedColumnName'];
@@ -21,7 +21,7 @@
/**
* @Annotation
- * @Target("PROPERTY")
+ * @Target({"PROPERTY","ANNOTATION"})
*/
final class Column implements Annotation
{
@@ -24,7 +24,8 @@
Doctrine\Common\Annotations\AnnotationRegistry,
Doctrine\ORM\Mapping\ClassMetadataInfo,
Doctrine\ORM\Mapping\MappingException,
- Doctrine\ORM\Mapping\JoinColumn;
+ Doctrine\ORM\Mapping\JoinColumn,
+ Doctrine\ORM\Mapping\Column;
/**
* The AnnotationDriver reads the mapping metadata from docblock annotations.
@@ -135,7 +136,7 @@ public function setFileExtension($fileExtension)
public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
{
$class = $metadata->getReflectionClass();
- if (!$class) {
+ if ( ! $class) {
// this happens when running annotation driver in combination with
// static reflection services. This is not the nicest fix
$class = new \ReflectionClass($metadata->name);
@@ -261,12 +262,12 @@ public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
if (isset($classAnnotations['Doctrine\ORM\Mapping\NamedQueries'])) {
$namedQueriesAnnot = $classAnnotations['Doctrine\ORM\Mapping\NamedQueries'];
- if (!is_array($namedQueriesAnnot->value)) {
+ if ( ! is_array($namedQueriesAnnot->value)) {
throw new \UnexpectedValueException("@NamedQueries should contain an array of @NamedQuery annotations.");
}
foreach ($namedQueriesAnnot->value as $namedQuery) {
- if (!($namedQuery instanceof \Doctrine\ORM\Mapping\NamedQuery)) {
+ if ( ! ($namedQuery instanceof \Doctrine\ORM\Mapping\NamedQuery)) {
throw new \UnexpectedValueException("@NamedQueries should contain an array of @NamedQuery annotations.");
}
$metadata->addNamedQuery(array(
@@ -378,23 +379,7 @@ public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
throw MappingException::propertyTypeIsRequired($className, $property->getName());
}
- $mapping['type'] = $columnAnnot->type;
- $mapping['length'] = $columnAnnot->length;
- $mapping['precision'] = $columnAnnot->precision;
- $mapping['scale'] = $columnAnnot->scale;
- $mapping['nullable'] = $columnAnnot->nullable;
- $mapping['unique'] = $columnAnnot->unique;
- if ($columnAnnot->options) {
- $mapping['options'] = $columnAnnot->options;
- }
-
- if (isset($columnAnnot->name)) {
- $mapping['columnName'] = $columnAnnot->name;
- }
-
- if (isset($columnAnnot->columnDefinition)) {
- $mapping['columnDefinition'] = $columnAnnot->columnDefinition;
- }
+ $mapping = $this->columnToArray($property->getName(), $columnAnnot);
if ($idAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Id')) {
$mapping['id'] = true;
@@ -537,6 +522,16 @@ public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
}
}
+ $attributeOverrides = array();
+ // Evaluate AttributeOverrides annotation
+ if (isset($classAnnotations['Doctrine\ORM\Mapping\AttributeOverrides'])) {
+ $attributeOverridesAnnot = $classAnnotations['Doctrine\ORM\Mapping\AttributeOverrides'];
+ foreach ($attributeOverridesAnnot->value as $attributeOverrideAnnot) {
+ $attributeOverride = $this->columnToArray($attributeOverrideAnnot->name, $attributeOverrideAnnot->column);
+ $metadata->setAttributeOverride($attributeOverrideAnnot->name, $attributeOverride);
+ }
+ }
+
// Evaluate @HasLifecycleCallbacks annotation
if (isset($classAnnotations['Doctrine\ORM\Mapping\HasLifecycleCallbacks'])) {
foreach ($class->getMethods() as $method) {
@@ -625,7 +620,7 @@ public function getAllClassNames()
return $this->_classNames;
}
- if (!$this->_paths) {
+ if ( ! $this->_paths) {
throw MappingException::pathRequired();
}
@@ -680,7 +675,7 @@ public function getAllClassNames()
*/
private function getFetchMode($className, $fetchMode)
{
- if(!defined('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $fetchMode)) {
+ if( ! defined('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $fetchMode)) {
throw MappingException::invalidFetchMode($className, $fetchMode);
}
@@ -706,6 +701,40 @@ private function joinColumnToArray(JoinColumn $joinColumn)
}
/**
+ * Parse the given Column as array
+ *
+ * @param string $fieldName
+ * @param Column $column
+ * @return array
+ */
+ private function columnToArray($fieldName, Column $column)
+ {
+ $mapping = array(
+ 'fieldName' => $fieldName,
+ 'type' => $column->type,
+ 'scale' => $column->scale,
+ 'length' => $column->length,
+ 'unique' => $column->unique,
+ 'nullable' => $column->nullable,
+ 'precision' => $column->precision
+ );
+
+ if ($column->options) {
+ $mapping['options'] = $column->options;
+ }
+
+ if (isset($column->name)) {
+ $mapping['columnName'] = $column->name;
+ }
+
+ if (isset($column->columnDefinition)) {
+ $mapping['columnDefinition'] = $column->columnDefinition;
+ }
+
+ return $mapping;
+ }
+
+ /**
* Factory method for the Annotation Driver
*
* @param array|string $paths
@@ -61,4 +61,7 @@
require_once __DIR__.'/../SqlResultSetMapping.php';
require_once __DIR__.'/../SqlResultSetMappings.php';
require_once __DIR__.'/../AssociationOverride.php';
-require_once __DIR__.'/../AssociationOverrides.php';
+require_once __DIR__.'/../AssociationOverrides.php';
+require_once __DIR__.'/../AssociationOverrides.php';
+require_once __DIR__.'/../AttributeOverride.php';
+require_once __DIR__.'/../AttributeOverrides.php';
Oops, something went wrong.

0 comments on commit 85790f0

Please sign in to comment.