Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement custom options on table level

  • Loading branch information...
commit d68fcd8bd20511f55f9ebdfe24d589db7cfc464b 1 parent 35fc3c0
@jsor jsor authored
View
18 doctrine-mapping.xsd
@@ -86,6 +86,7 @@
<xs:complexType name="entity">
<xs:sequence>
+ <xs:element name="options" type="orm:options" minOccurs="0" />
<xs:element name="indexes" type="orm:indexes" minOccurs="0"/>
<xs:element name="unique-constraints" type="orm:unique-constraints" minOccurs="0"/>
<xs:element name="discriminator-column" type="orm:discriminator-column" minOccurs="0"/>
@@ -110,6 +111,23 @@
<xs:anyAttribute namespace="##other"/>
</xs:complexType>
+ <xs:complexType name="option" mixed="true">
+ <xs:sequence minOccurs="0" maxOccurs="unbounded">
+ <xs:element name="option" type="orm:option"/>
+ <xs:any minOccurs="0" maxOccurs="unbounded" namespace="##other"/>
+ </xs:sequence>
+ <xs:attribute name="name" type="xs:NMTOKEN" use="required"/>
+ <xs:anyAttribute namespace="##other"/>
+ </xs:complexType>
+
+ <xs:complexType name="options">
+ <xs:sequence>
+ <xs:element name="option" type="orm:option" minOccurs="0" maxOccurs="unbounded"/>
+ <xs:any minOccurs="0" maxOccurs="unbounded" namespace="##other"/>
+ </xs:sequence>
+ <xs:anyAttribute namespace="##other"/>
+ </xs:complexType>
+
<xs:complexType name="mapped-superclass" >
<xs:complexContent>
<xs:extension base="orm:entity">
View
4 lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
@@ -1705,6 +1705,10 @@ public function setPrimaryTable(array $table)
if (isset($table['uniqueConstraints'])) {
$this->table['uniqueConstraints'] = $table['uniqueConstraints'];
}
+
+ if (isset($table['options'])) {
+ $this->table['options'] = $table['options'];
+ }
}
/**
View
4 lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php
@@ -190,6 +190,10 @@ public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
}
}
+ if ($tableAnnot->options !== null) {
+ $primaryTable['options'] = $tableAnnot->options;
+ }
+
$metadata->setPrimaryTable($primaryTable);
}
View
33 lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php
@@ -161,6 +161,10 @@ public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
}
}
+ if (isset($xmlRoot->options)) {
+ $metadata->table['options'] = $this->_parseOptions($xmlRoot->options->children());
+ }
+
// Evaluate <field ...> mappings
if (isset($xmlRoot->field)) {
foreach ($xmlRoot->field as $fieldMapping) {
@@ -455,6 +459,35 @@ public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
}
/**
+ * Parses (nested) option elements.
+ *
+ * @param $options The XML element.
+ * @return array The options array.
+ */
+ private function _parseOptions(SimpleXMLElement $options)
+ {
+ $array = array();
+
+ foreach ($options as $option) {
+ if ($option->count()) {
+ $value = $this->_parseOptions($option->children());
+ } else {
+ $value = (string) $option;
+ }
+
+ $attr = $option->attributes();
+
+ if (isset($attr->name)) {
+ $array[(string) $attr->name] = $value;
+ } else {
+ $array[] = $value;
+ }
+ }
+
+ return $array;
+ }
+
+ /**
* Constructs a joinColumn mapping array based on the information
* found in the given SimpleXMLElement.
*
View
4 lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php
@@ -156,6 +156,10 @@ public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
}
}
+ if (isset($element['options'])) {
+ $metadata->table['options'] = $element['options'];
+ }
+
$associationIds = array();
if (isset($element['id'])) {
// Evaluate identifier settings
View
2  lib/Doctrine/ORM/Mapping/Table.php
@@ -33,4 +33,6 @@
public $indexes;
/** @var array<\Doctrine\ORM\Mapping\UniqueConstraint> */
public $uniqueConstraints;
+ /** @var array */
+ public $options = array();
}
View
6 lib/Doctrine/ORM/Tools/SchemaTool.php
@@ -235,6 +235,12 @@ public function getSchemaFromMetadata(array $classes)
}
}
+ if (isset($class->table['options'])) {
+ foreach ($class->table['options'] AS $key => $val) {
+ $table->addOption($key, $val);
+ }
+ }
+
$processedClasses[$class->name] = true;
if ($class->isIdGeneratorSequence() && $class->name == $class->rootEntityName) {
View
19 tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php
@@ -77,6 +77,21 @@ public function testEntityUniqueConstraints($class)
* @depends testEntityTableNameAndInheritance
* @param ClassMetadata $class
*/
+ public function testEntityOptions($class)
+ {
+ $this->assertArrayHasKey('options', $class->table, 'ClassMetadata should have options key in table property.');
+
+ $this->assertEquals(array(
+ 'foo' => 'bar', 'baz' => array('key' => 'val')
+ ), $class->table['options']);
+
+ return $class;
+ }
+
+ /**
+ * @depends testEntityOptions
+ * @param ClassMetadata $class
+ */
public function testEntitySequence($class)
{
$this->assertInternalType('array', $class->sequenceGeneratorDefinition, 'No Sequence Definition set on this driver.');
@@ -424,7 +439,8 @@ public function testNamingStrategy()
* @Table(
* name="cms_users",
* uniqueConstraints={@UniqueConstraint(name="search_idx", columns={"name", "user_email"})},
- * indexes={@Index(name="name_idx", columns={"name"}), @Index(name="0", columns={"user_email"})}
+ * indexes={@Index(name="name_idx", columns={"name"}), @Index(name="0", columns={"user_email"})},
+ * options={"foo": "bar", "baz": {"key": "val"}}
* )
*/
class User
@@ -495,6 +511,7 @@ public static function loadMetadata(ClassMetadataInfo $metadata)
$metadata->setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_NONE);
$metadata->setPrimaryTable(array(
'name' => 'cms_users',
+ 'options' => array('foo' => 'bar', 'baz' => array('key' => 'val')),
));
$metadata->setChangeTrackingPolicy(ClassMetadataInfo::CHANGETRACKING_DEFERRED_IMPLICIT);
$metadata->addLifecycleCallback('doStuffOnPrePersist', 'prePersist');
View
4 tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.User.php
@@ -106,6 +106,10 @@
),
'orderBy' => NULL,
));
+$metadata->table['options'] = array(
+ 'foo' => 'bar',
+ 'baz' => array('key' => 'val')
+);
$metadata->table['uniqueConstraints'] = array(
'search_idx' => array('columns' => array('name', 'user_email')),
);
View
8 tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.User.dcm.xml
@@ -6,6 +6,12 @@
http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Doctrine\Tests\ORM\Mapping\User" table="cms_users">
+ <options>
+ <option name="foo">bar</option>
+ <option name="baz">
+ <option name="key">val</option>
+ </option>
+ </options>
<indexes>
<index name="name_idx" columns="name"/>
@@ -15,7 +21,7 @@
<unique-constraints>
<unique-constraint columns="name,user_email" name="search_idx" />
</unique-constraints>
-
+
<lifecycle-callbacks>
<lifecycle-callback type="prePersist" method="doStuffOnPrePersist"/>
<lifecycle-callback type="prePersist" method="doOtherStuffOnPrePersistToo"/>
View
4 tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.User.dcm.yml
@@ -1,6 +1,10 @@
Doctrine\Tests\ORM\Mapping\User:
type: entity
table: cms_users
+ options:
+ foo: bar
+ baz:
+ key: val
namedQueries:
all: SELECT u FROM __CLASS__ u
id:
View
11 tests/Doctrine/Tests/ORM/Tools/SchemaToolTest.php
@@ -32,19 +32,21 @@ public function testAddUniqueIndexForUniqueFieldAnnocation()
$this->assertTrue($schema->getTable('cms_users')->columnsAreIndexed(array('username')), "username column should be indexed.");
}
- public function testColumnAnnotationOptionsAttribute()
+ public function testAnnotationOptionsAttribute()
{
$em = $this->_getTestEntityManager();
$schemaTool = new SchemaTool($em);
$classes = array(
- $em->getClassMetadata(__NAMESPACE__ . '\\TestEntityWithColumnAnnotationOptionsAttribute'),
+ $em->getClassMetadata(__NAMESPACE__ . '\\TestEntityWithAnnotationOptionsAttribute'),
);
$schema = $schemaTool->getSchemaFromMetadata($classes);
$expected = array('foo' => 'bar', 'baz' => array('key' => 'val'));
- $this->assertEquals($expected, $schema->getTable('TestEntityWithColumnAnnotationOptionsAttribute')->getColumn('test')->getCustomSchemaOptions(), "options annotation are passed to the columns customSchemaOptions");
+
+ $this->assertEquals($expected, $schema->getTable('TestEntityWithAnnotationOptionsAttribute')->getOptions(), "options annotation are passed to the tables optionss");
+ $this->assertEquals($expected, $schema->getTable('TestEntityWithAnnotationOptionsAttribute')->getColumn('test')->getCustomSchemaOptions(), "options annotation are passed to the columns customSchemaOptions");
}
/**
@@ -103,8 +105,9 @@ public function testPostGenerateEvents()
/**
* @Entity
+ * @Table(options={"foo": "bar", "baz": {"key": "val"}})
*/
-class TestEntityWithColumnAnnotationOptionsAttribute
+class TestEntityWithAnnotationOptionsAttribute
{
/** @Id @Column */
private $id;
Please sign in to comment.
Something went wrong with that request. Please try again.