Commit ee9aa005 authored by beberlei's avatar beberlei

[2.0] DDC-271 - Add columnDefinition for Join-Column in Annotation, XML and...

[2.0] DDC-271 - Add columnDefinition for Join-Column in Annotation, XML and YAML driver, updated SchemaTool to use Join Column definition instead of using the column's columnDefinition if specified. Refactored MappingDriverTest to allow for feature specific test-cases instead of one large assertion block. Fixed typos in XmlDriver - Updated doctrine-mapping.xsd Schema File
parent 2fa58ee7
...@@ -123,7 +123,7 @@ ...@@ -123,7 +123,7 @@
<xs:attribute name="unique" type="xs:boolean" default="false" /> <xs:attribute name="unique" type="xs:boolean" default="false" />
<xs:attribute name="nullable" type="xs:boolean" default="false" /> <xs:attribute name="nullable" type="xs:boolean" default="false" />
<xs:attribute name="version" type="xs:boolean" /> <xs:attribute name="version" type="xs:boolean" />
<xs:attribute name="columnDefinition" type="xs:string" /> <xs:attribute name="column-definition" type="xs:string" />
</xs:complexType> </xs:complexType>
<xs:complexType name="discriminator-column"> <xs:complexType name="discriminator-column">
...@@ -192,6 +192,7 @@ ...@@ -192,6 +192,7 @@
<xs:attribute name="nullable" type="xs:boolean" default="true" /> <xs:attribute name="nullable" type="xs:boolean" default="true" />
<xs:attribute name="on-delete" type="orm:fk-action" /> <xs:attribute name="on-delete" type="orm:fk-action" />
<xs:attribute name="on-update" type="orm:fk-action" /> <xs:attribute name="on-update" type="orm:fk-action" />
<xs:attribute name="column-definition" type="xs:string" />
</xs:complexType> </xs:complexType>
<xs:complexType name="join-columns"> <xs:complexType name="join-columns">
......
...@@ -218,7 +218,8 @@ class AnnotationDriver implements Driver ...@@ -218,7 +218,8 @@ class AnnotationDriver implements Driver
'unique' => $joinColumnAnnot->unique, 'unique' => $joinColumnAnnot->unique,
'nullable' => $joinColumnAnnot->nullable, 'nullable' => $joinColumnAnnot->nullable,
'onDelete' => $joinColumnAnnot->onDelete, 'onDelete' => $joinColumnAnnot->onDelete,
'onUpdate' => $joinColumnAnnot->onUpdate 'onUpdate' => $joinColumnAnnot->onUpdate,
'columnDefinition' => $joinColumnAnnot->columnDefinition,
); );
} else if ($joinColumnsAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinColumns')) { } else if ($joinColumnsAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinColumns')) {
foreach ($joinColumnsAnnot->value as $joinColumn) { foreach ($joinColumnsAnnot->value as $joinColumn) {
...@@ -228,7 +229,8 @@ class AnnotationDriver implements Driver ...@@ -228,7 +229,8 @@ class AnnotationDriver implements Driver
'unique' => $joinColumn->unique, 'unique' => $joinColumn->unique,
'nullable' => $joinColumn->nullable, 'nullable' => $joinColumn->nullable,
'onDelete' => $joinColumn->onDelete, 'onDelete' => $joinColumn->onDelete,
'onUpdate' => $joinColumn->onUpdate 'onUpdate' => $joinColumn->onUpdate,
'columnDefinition' => $joinColumn->columnDefinition,
); );
} }
} }
...@@ -319,7 +321,8 @@ class AnnotationDriver implements Driver ...@@ -319,7 +321,8 @@ class AnnotationDriver implements Driver
'unique' => $joinColumn->unique, 'unique' => $joinColumn->unique,
'nullable' => $joinColumn->nullable, 'nullable' => $joinColumn->nullable,
'onDelete' => $joinColumn->onDelete, 'onDelete' => $joinColumn->onDelete,
'onUpdate' => $joinColumn->onUpdate 'onUpdate' => $joinColumn->onUpdate,
'columnDefinition' => $joinColumn->columnDefinition,
); );
} }
...@@ -330,7 +333,8 @@ class AnnotationDriver implements Driver ...@@ -330,7 +333,8 @@ class AnnotationDriver implements Driver
'unique' => $joinColumn->unique, 'unique' => $joinColumn->unique,
'nullable' => $joinColumn->nullable, 'nullable' => $joinColumn->nullable,
'onDelete' => $joinColumn->onDelete, 'onDelete' => $joinColumn->onDelete,
'onUpdate' => $joinColumn->onUpdate 'onUpdate' => $joinColumn->onUpdate,
'columnDefinition' => $joinColumn->columnDefinition,
); );
} }
} }
......
...@@ -51,6 +51,7 @@ final class JoinColumn extends Annotation { ...@@ -51,6 +51,7 @@ final class JoinColumn extends Annotation {
public $nullable = true; public $nullable = true;
public $onDelete; public $onDelete;
public $onUpdate; public $onUpdate;
public $columnDefinition;
} }
final class JoinColumns extends Annotation {} final class JoinColumns extends Annotation {}
final class Column extends Annotation { final class Column extends Annotation {
......
...@@ -164,8 +164,8 @@ class XmlDriver extends AbstractFileDriver ...@@ -164,8 +164,8 @@ class XmlDriver extends AbstractFileDriver
$metadata->setVersionMapping($mapping); $metadata->setVersionMapping($mapping);
} }
if (isset($fieldMapping['columnDefinition'])) { if (isset($fieldMapping['column-definition'])) {
$mapping['columnDefinition'] = (string)$fieldMapping['columnDefinition']; $mapping['columnDefinition'] = (string)$fieldMapping['column-definition'];
} }
$metadata->mapField($mapping); $metadata->mapField($mapping);
...@@ -392,13 +392,17 @@ class XmlDriver extends AbstractFileDriver ...@@ -392,13 +392,17 @@ class XmlDriver extends AbstractFileDriver
$joinColumn['nullable'] = ((string)$joinColumnElement['nullable'] == "false") ? false : true; $joinColumn['nullable'] = ((string)$joinColumnElement['nullable'] == "false") ? false : true;
} }
if (isset($joinColumnElement['onDelete'])) { if (isset($joinColumnElement['on-delete'])) {
$joinColumn['onDelete'] = (string)$joinColumnElement['on-delete']; $joinColumn['onDelete'] = (string)$joinColumnElement['on-delete'];
} }
if (isset($joinColumnElement['onUpdate'])) { if (isset($joinColumnElement['on-update'])) {
$joinColumn['onUpdate'] = (string)$joinColumnElement['on-update']; $joinColumn['onUpdate'] = (string)$joinColumnElement['on-update'];
} }
if (isset($joinColumnElement['column-definition'])) {
$joinColumn['columnDefinition'] = (string)$joinColumnElement['column-definition'];
}
return $joinColumn; return $joinColumn;
} }
......
...@@ -414,6 +414,10 @@ class YamlDriver extends AbstractFileDriver ...@@ -414,6 +414,10 @@ class YamlDriver extends AbstractFileDriver
if (isset($joinColumnElement['onUpdate'])) { if (isset($joinColumnElement['onUpdate'])) {
$joinColumn['onUpdate'] = $joinColumnElement['onUpdate']; $joinColumn['onUpdate'] = $joinColumnElement['onUpdate'];
} }
if (isset($joinColumnElement['columnDefinition'])) {
$joinColumn['columnDefinition'] = $joinColumnElement['columnDefinition'];
}
return $joinColumn; return $joinColumn;
} }
......
...@@ -415,7 +415,13 @@ class SchemaTool ...@@ -415,7 +415,13 @@ class SchemaTool
// property as well. // property as well.
$fieldMapping = $class->getFieldMapping($referencedFieldName); $fieldMapping = $class->getFieldMapping($referencedFieldName);
$columnDef = isset($fieldMapping['columnDefinition']) ? $fieldMapping['columnDefinition'] : null;
$columnDef = null;
if (isset($joinColumn['columnDefinition'])) {
$columnDef = $joinColumn['columnDefinition'];
} else if (isset($fieldMapping['columnDefinition'])) {
$columnDef = $fieldMapping['columnDefinition'];
}
$columnOptions = array('notnull' => false, 'columnDefinition' => $columnDef); $columnOptions = array('notnull' => false, 'columnDefinition' => $columnDef);
$theJoinTable->createColumn( $theJoinTable->createColumn(
......
...@@ -7,66 +7,86 @@ use Doctrine\ORM\Mapping\ClassMetadata, ...@@ -7,66 +7,86 @@ use Doctrine\ORM\Mapping\ClassMetadata,
Doctrine\ORM\Mapping\Driver\YamlDriver; Doctrine\ORM\Mapping\Driver\YamlDriver;
require_once __DIR__ . '/../../TestInit.php'; require_once __DIR__ . '/../../TestInit.php';
class MappingDriverTest extends \Doctrine\Tests\OrmTestCase abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase
{ {
public function testXmlMapping() abstract protected function _loadDriver();
{
$className = 'Doctrine\Tests\ORM\Mapping\User'; public function testLoadMapping()
$xmlDriver = new XmlDriver(__DIR__ . DIRECTORY_SEPARATOR . 'xml');
$class = new ClassMetadata($className);
$this->assertFalse($xmlDriver->isTransient($className));
$xmlDriver->loadMetadataForClass($className, $class);
$this->_testUserClassMapping($class);
}
public function testYamlMapping()
{ {
$className = 'Doctrine\Tests\ORM\Mapping\User'; $className = 'Doctrine\Tests\ORM\Mapping\User';
$yamlDriver = new YamlDriver(__DIR__ . DIRECTORY_SEPARATOR . 'yaml'); $mappingDriver = $this->_loadDriver();
$class = new ClassMetadata($className); $class = new ClassMetadata($className);
$this->assertFalse($yamlDriver->isTransient($className)); $this->assertFalse($mappingDriver->isTransient($className));
$yamlDriver->loadMetadataForClass($className, $class); $mappingDriver->loadMetadataForClass($className, $class);
$this->_testUserClassMapping($class); return $class;
} }
public function testXmlGetAllClassNames() /**
* @depends testLoadMapping
* @param ClassMetadata $class
*/
public function testEntityTableNameAndInheritance($class)
{ {
$className = 'Doctrine\Tests\ORM\Mapping\User';
$xmlDriver = new XmlDriver(__DIR__ . DIRECTORY_SEPARATOR . 'xml');
$class = new ClassMetadata($className);
$classNames = $xmlDriver->getAllClassNames();
$this->assertEquals($className, $classNames[0]);
$this->assertEquals(1, count($classNames));
}
private function _testUserClassMapping(ClassMetadata $class)
{
$this->assertEquals('cms_users', $class->getTableName()); $this->assertEquals('cms_users', $class->getTableName());
$this->assertEquals(ClassMetadata::INHERITANCE_TYPE_NONE, $class->getInheritanceType()); $this->assertEquals(ClassMetadata::INHERITANCE_TYPE_NONE, $class->getInheritanceType());
$this->assertEquals(2, count($class->fieldMappings));
return $class;
}
/**
* @depends testEntityTableNameAndInheritance
* @param ClassMetadata $class
*/
public function testFieldMappings($class)
{
$this->assertEquals(3, count($class->fieldMappings));
$this->assertTrue(isset($class->fieldMappings['id'])); $this->assertTrue(isset($class->fieldMappings['id']));
$this->assertTrue(isset($class->fieldMappings['name'])); $this->assertTrue(isset($class->fieldMappings['name']));
$this->assertTrue(isset($class->fieldMappings['email']));
$this->assertEquals('string', $class->fieldMappings['name']['type']); $this->assertEquals('string', $class->fieldMappings['name']['type']);
$this->assertTrue($class->fieldMappings['name']['nullable']); $this->assertTrue($class->fieldMappings['name']['nullable']);
$this->assertTrue($class->fieldMappings['name']['unique']); $this->assertTrue($class->fieldMappings['name']['unique']);
$this->assertEquals("user_email", $class->fieldMappings['email']['columnName']);
return $class;
}
/**
* @depends testFieldMappings
* @param ClassMetadata $class
*/
public function testIdentifier($class)
{
$this->assertEquals(array('id'), $class->identifier); $this->assertEquals(array('id'), $class->identifier);
$this->assertEquals(ClassMetadata::GENERATOR_TYPE_AUTO, $class->getIdGeneratorType()); $this->assertEquals(ClassMetadata::GENERATOR_TYPE_AUTO, $class->getIdGeneratorType());
return $class;
}
/**
* @depends testIdentifier
* @param ClassMetadata $class
*/
public function testAssocations($class)
{
$this->assertEquals(3, count($class->associationMappings)); $this->assertEquals(3, count($class->associationMappings));
$this->assertEquals(1, count($class->inverseMappings)); $this->assertEquals(1, count($class->inverseMappings));
return $class;
}
/**
* @depends testAssocations
* @param ClassMetadata $class
*/
public function testOwningOneToOneAssocation($class)
{
$this->assertTrue($class->associationMappings['address'] instanceof \Doctrine\ORM\Mapping\OneToOneMapping); $this->assertTrue($class->associationMappings['address'] instanceof \Doctrine\ORM\Mapping\OneToOneMapping);
$this->assertTrue(isset($class->associationMappings['address'])); $this->assertTrue(isset($class->associationMappings['address']));
$this->assertTrue($class->associationMappings['address']->isOwningSide); $this->assertTrue($class->associationMappings['address']->isOwningSide);
...@@ -76,7 +96,16 @@ class MappingDriverTest extends \Doctrine\Tests\OrmTestCase ...@@ -76,7 +96,16 @@ class MappingDriverTest extends \Doctrine\Tests\OrmTestCase
$this->assertFalse($class->associationMappings['address']->isCascadeRefresh); $this->assertFalse($class->associationMappings['address']->isCascadeRefresh);
$this->assertFalse($class->associationMappings['address']->isCascadeDetach); $this->assertFalse($class->associationMappings['address']->isCascadeDetach);
$this->assertFalse($class->associationMappings['address']->isCascadeMerge); $this->assertFalse($class->associationMappings['address']->isCascadeMerge);
return $class;
}
/**
* @depends testOwningOneToOneAssocation
* @param ClassMetadata $class
*/
public function testInverseOneToManyAssociation($class)
{
$this->assertTrue($class->associationMappings['phonenumbers'] instanceof \Doctrine\ORM\Mapping\OneToManyMapping); $this->assertTrue($class->associationMappings['phonenumbers'] instanceof \Doctrine\ORM\Mapping\OneToManyMapping);
$this->assertTrue(isset($class->associationMappings['phonenumbers'])); $this->assertTrue(isset($class->associationMappings['phonenumbers']));
$this->assertFalse($class->associationMappings['phonenumbers']->isOwningSide); $this->assertFalse($class->associationMappings['phonenumbers']->isOwningSide);
...@@ -86,13 +115,19 @@ class MappingDriverTest extends \Doctrine\Tests\OrmTestCase ...@@ -86,13 +115,19 @@ class MappingDriverTest extends \Doctrine\Tests\OrmTestCase
$this->assertFalse($class->associationMappings['phonenumbers']->isCascadeRefresh); $this->assertFalse($class->associationMappings['phonenumbers']->isCascadeRefresh);
$this->assertFalse($class->associationMappings['phonenumbers']->isCascadeDetach); $this->assertFalse($class->associationMappings['phonenumbers']->isCascadeDetach);
$this->assertFalse($class->associationMappings['phonenumbers']->isCascadeMerge); $this->assertFalse($class->associationMappings['phonenumbers']->isCascadeMerge);
return $class;
}
/**
* @depends testInverseOneToManyAssociation
* @param ClassMetadata $class
*/
public function testManyToManyAssociationWithCascadeAll($class)
{
$this->assertTrue($class->associationMappings['groups'] instanceof \Doctrine\ORM\Mapping\ManyToManyMapping); $this->assertTrue($class->associationMappings['groups'] instanceof \Doctrine\ORM\Mapping\ManyToManyMapping);
$this->assertTrue(isset($class->associationMappings['groups'])); $this->assertTrue(isset($class->associationMappings['groups']));
$this->assertTrue($class->associationMappings['groups']->isOwningSide); $this->assertTrue($class->associationMappings['groups']->isOwningSide);
$this->assertEquals(count($class->lifecycleCallbacks), 2);
$this->assertEquals($class->lifecycleCallbacks['prePersist'][0], 'doStuffOnPrePersist');
$this->assertEquals($class->lifecycleCallbacks['postPersist'][0], 'doStuffOnPostPersist');
// Make sure that cascade-all works as expected // Make sure that cascade-all works as expected
$this->assertTrue($class->associationMappings['groups']->isCascadeRemove); $this->assertTrue($class->associationMappings['groups']->isCascadeRemove);
$this->assertTrue($class->associationMappings['groups']->isCascadePersist); $this->assertTrue($class->associationMappings['groups']->isCascadePersist);
...@@ -100,19 +135,56 @@ class MappingDriverTest extends \Doctrine\Tests\OrmTestCase ...@@ -100,19 +135,56 @@ class MappingDriverTest extends \Doctrine\Tests\OrmTestCase
$this->assertTrue($class->associationMappings['groups']->isCascadeDetach); $this->assertTrue($class->associationMappings['groups']->isCascadeDetach);
$this->assertTrue($class->associationMappings['groups']->isCascadeMerge); $this->assertTrue($class->associationMappings['groups']->isCascadeMerge);
return $class;
}
/**
* @depends testManyToManyAssociationWithCascadeAll
* @param ClassMetadata $class
*/
public function testLifecycleCallbacks($class)
{
$this->assertEquals(count($class->lifecycleCallbacks), 2);
$this->assertEquals($class->lifecycleCallbacks['prePersist'][0], 'doStuffOnPrePersist');
$this->assertEquals($class->lifecycleCallbacks['postPersist'][0], 'doStuffOnPostPersist');
return $class;
}
/**
* @depends testLifecycleCallbacks
* @param ClassMetadata $class
*/
public function testJoinColumnUniqueAndNullable($class)
{
// Non-Nullability of Join Column // Non-Nullability of Join Column
$this->assertFalse($class->associationMappings['groups']->joinTable['joinColumns'][0]['nullable']); $this->assertFalse($class->associationMappings['groups']->joinTable['joinColumns'][0]['nullable']);
$this->assertFalse($class->associationMappings['groups']->joinTable['joinColumns'][0]['unique']); $this->assertFalse($class->associationMappings['groups']->joinTable['joinColumns'][0]['unique']);
return $class;
}
/**
* @depends testJoinColumnUniqueAndNullable
* @param ClassMetadata $class
*/
public function testColumnDefinition($class)
{
$this->assertEquals("CHAR(32) NOT NULL", $class->fieldMappings['email']['columnDefinition']);
$this->assertEquals("INT NULL", $class->associationMappings['groups']->joinTable['inverseJoinColumns'][0]['columnDefinition']);
return $class;
} }
} }
class User { class User {
private $id; private $id;
private $name; private $name;
private $email;
private $address; private $address;
private $phonenumbers; private $phonenumbers;
private $groups; private $groups;
// ... rest of code omitted, irrelevant for the mapping tests // ... rest of code omitted, irrelevant for the mapping tests
public function doStuffOnPrePersist() public function doStuffOnPrePersist()
...@@ -121,6 +193,6 @@ class User { ...@@ -121,6 +193,6 @@ class User {
public function doStuffOnPostPersist() public function doStuffOnPostPersist()
{ {
} }
} }
\ No newline at end of file
...@@ -20,7 +20,8 @@ class AllTests ...@@ -20,7 +20,8 @@ class AllTests
$suite = new \Doctrine\Tests\DoctrineTestSuite('Doctrine Orm Mapping'); $suite = new \Doctrine\Tests\DoctrineTestSuite('Doctrine Orm Mapping');
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\MappingDriverTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Mapping\XmlMappingDriverTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\YamlMappingDriverTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataFactoryTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataFactoryTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataLoadEventTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataLoadEventTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\BasicInheritanceMappingTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Mapping\BasicInheritanceMappingTest');
......
<?php
namespace Doctrine\Tests\ORM\Mapping;
use Doctrine\ORM\Mapping\ClassMetadata,
Doctrine\ORM\Mapping\Driver\XmlDriver,
Doctrine\ORM\Mapping\Driver\YamlDriver;
require_once __DIR__ . '/../../TestInit.php';
class XmlMappingDriverTest extends AbstractMappingDriverTest
{
protected function _loadDriver()
{
return new XmlDriver(__DIR__ . DIRECTORY_SEPARATOR . 'xml');
}
}
\ No newline at end of file
<?php
namespace Doctrine\Tests\ORM\Mapping;
use Doctrine\ORM\Mapping\ClassMetadata,
Doctrine\ORM\Mapping\Driver\XmlDriver,
Doctrine\ORM\Mapping\Driver\YamlDriver;
require_once __DIR__ . '/../../TestInit.php';
class YamlMappingDriverTest extends AbstractMappingDriverTest
{
protected function _loadDriver()
{
return new YamlDriver(__DIR__ . DIRECTORY_SEPARATOR . 'yaml');
}
}
\ No newline at end of file
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
</id> </id>
<field name="name" column="name" type="string" length="50" nullable="true" unique="true" /> <field name="name" column="name" type="string" length="50" nullable="true" unique="true" />
<field name="email" column="user_email" type="string" column-definition="CHAR(32) NOT NULL" />
<one-to-one field="address" target-entity="Address"> <one-to-one field="address" target-entity="Address">
<join-column name="address_id" referenced-column-name="id"/> <join-column name="address_id" referenced-column-name="id"/>
...@@ -37,7 +38,7 @@ ...@@ -37,7 +38,7 @@
<join-column name="user_id" referenced-column-name="id" nullable="false" unique="false" /> <join-column name="user_id" referenced-column-name="id" nullable="false" unique="false" />
</join-columns> </join-columns>
<inverse-join-columns> <inverse-join-columns>
<join-column name="group_id" referenced-column-name="id"/> <join-column name="group_id" referenced-column-name="id" column-definition="INT NULL" />
</inverse-join-columns> </inverse-join-columns>
</join-table> </join-table>
<cascade> <cascade>
......
...@@ -12,6 +12,10 @@ Doctrine\Tests\ORM\Mapping\User: ...@@ -12,6 +12,10 @@ Doctrine\Tests\ORM\Mapping\User:
length: 50 length: 50
nullable: true nullable: true
unique: true unique: true
email:
type: string
column: user_email
columnDefinition: CHAR(32) NOT NULL
oneToOne: oneToOne:
address: address:
targetEntity: Address targetEntity: Address
...@@ -37,6 +41,7 @@ Doctrine\Tests\ORM\Mapping\User: ...@@ -37,6 +41,7 @@ Doctrine\Tests\ORM\Mapping\User:
inverseJoinColumns: inverseJoinColumns:
group_id: group_id:
referencedColumnName: id referencedColumnName: id
columnDefinition: INT NULL
cascade: cascade:
- all - all
lifecycleCallbacks: lifecycleCallbacks:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment