1
0
mirror of synced 2024-12-05 03:06:05 +03:00

Implement custom options on table level

This commit is contained in:
Jan Sorgalla 2012-01-26 14:36:56 +01:00
parent 35fc3c0671
commit d68fcd8bd2
12 changed files with 111 additions and 6 deletions

View File

@ -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 File

@ -1705,6 +1705,10 @@ class ClassMetadataInfo implements ClassMetadata
if (isset($table['uniqueConstraints'])) {
$this->table['uniqueConstraints'] = $table['uniqueConstraints'];
}
if (isset($table['options'])) {
$this->table['options'] = $table['options'];
}
}
/**

View File

@ -190,6 +190,10 @@ class AnnotationDriver implements Driver
}
}
if ($tableAnnot->options !== null) {
$primaryTable['options'] = $tableAnnot->options;
}
$metadata->setPrimaryTable($primaryTable);
}

View File

@ -161,6 +161,10 @@ class XmlDriver extends AbstractFileDriver
}
}
if (isset($xmlRoot->options)) {
$metadata->table['options'] = $this->_parseOptions($xmlRoot->options->children());
}
// Evaluate <field ...> mappings
if (isset($xmlRoot->field)) {
foreach ($xmlRoot->field as $fieldMapping) {
@ -454,6 +458,35 @@ class XmlDriver extends AbstractFileDriver
}
}
/**
* 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 File

@ -156,6 +156,10 @@ class YamlDriver extends AbstractFileDriver
}
}
if (isset($element['options'])) {
$metadata->table['options'] = $element['options'];
}
$associationIds = array();
if (isset($element['id'])) {
// Evaluate identifier settings

View File

@ -33,4 +33,6 @@ final class Table implements Annotation
public $indexes;
/** @var array<\Doctrine\ORM\Mapping\UniqueConstraint> */
public $uniqueConstraints;
/** @var array */
public $options = array();
}

View File

@ -235,6 +235,12 @@ class SchemaTool
}
}
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 File

@ -77,6 +77,21 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase
* @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 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase
* @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 @@ class User
$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 File

@ -106,6 +106,10 @@ $metadata->mapManyToMany(array(
),
'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 File

@ -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 File

@ -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 File

@ -32,19 +32,21 @@ class SchemaToolTest extends \Doctrine\Tests\OrmTestCase
$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 @@ class SchemaToolTest extends \Doctrine\Tests\OrmTestCase
/**
* @Entity
* @Table(options={"foo": "bar", "baz": {"key": "val"}})
*/
class TestEntityWithColumnAnnotationOptionsAttribute
class TestEntityWithAnnotationOptionsAttribute
{
/** @Id @Column */
private $id;