From 88574a0de2fb9674d9caeb65197920e8644c2879 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 4 Sep 2011 01:11:19 +0200 Subject: [PATCH 1/3] Add note about Common Annotation changes --- UPGRADE_TO_2_2 | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/UPGRADE_TO_2_2 b/UPGRADE_TO_2_2 index 0500e4599..76372b221 100644 --- a/UPGRADE_TO_2_2 +++ b/UPGRADE_TO_2_2 @@ -1,3 +1,19 @@ # Removed support for onUpdate in @JoinColumn The onUpdate foreign key handling makes absolutly no sense in an ORM. Additionally Oracle doesn't even support it. Support for it is removed. + +# Changes in Annotation Handling + +There have been some changes to the annotation handling in Common 2.2 again, that affect how people with old configurations +from 2.0 have to configure the annotation driver if they don't use `Configuration::newDefaultAnnotationDriver()`: + + // Register the ORM Annotations in the AnnotationRegistry + AnnotationRegistry::registerFile('path/to/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'); + + $reader = new \Doctrine\Common\Annotations\SimpleAnnotationReader(); + $reader->addNamespace('Doctrine\ORM\Mapping'); + $reader = new \Doctrine\Common\Annotations\CachedReader($reader, new ArrayCache()); + + $driver = new AnnotationDriver($reader, (array)$paths); + + $config->setMetadataDriverImpl($driver); \ No newline at end of file From 342c3ba9419e2cb2dd77966188e0fd82dc870ecc Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 4 Sep 2011 10:08:33 +0200 Subject: [PATCH 2/3] Initial work on ClassMetdataBuilder --- .../Mapping/Builder/ClassMetadataBuilder.php | 79 +++++++++++++++ .../ORM/Mapping/ClassMetadataBuilderTest.php | 99 +++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100644 lib/Doctrine/ORM/Mapping/Builder/ClassMetadataBuilder.php create mode 100644 tests/Doctrine/Tests/ORM/Mapping/ClassMetadataBuilderTest.php diff --git a/lib/Doctrine/ORM/Mapping/Builder/ClassMetadataBuilder.php b/lib/Doctrine/ORM/Mapping/Builder/ClassMetadataBuilder.php new file mode 100644 index 000000000..b2a97e7f8 --- /dev/null +++ b/lib/Doctrine/ORM/Mapping/Builder/ClassMetadataBuilder.php @@ -0,0 +1,79 @@ +. + */ + +namespace Doctrine\ORM\Mapping\Builder; + +use Doctrine\ORM\Mapping\ClassMetadata; + +/** + * Builder Object for ClassMetadata + * + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.doctrine-project.com + * @since 2.0 + * @author Benjamin Eberlei + */ +class ClassMetadataBuilder +{ + /** + * @var ClassMetadata + */ + private $cm; + + public function __construct(ClassMetadata $cm) + { + $this->cm = $cm; + } + + public function setMappedSuperClass() + { + $this->cm->isMappedSuperclass = true; + } + + public function setCustomRepositoryClass($repositoryClassName) + { + $this->cm->setCustomRepositoryClass($repositoryClassName); + } + + public function setReadOnly() + { + $this->cm->markReadOnly(); + } + + public function setTable($name) + { + $this->cm->setPrimaryTable(array('name' => $name)); + } + + public function addIndex(array $columns, $name) + { + if (!isset($this->cm->table['indexes'])) { + $this->cm->table['indexes'] = array(); + } + $this->cm->table['indexes'][$name] = array('columns' => $columns); + } + + public function addUniqueConstraint(array $columns, $name) + { + if (!isset($this->cm->table['uniqueConstraints'])) { + $this->cm->table['uniqueConstraints'] = array(); + } + $this->cm->table['uniqueConstraints'][$name] = array('columns' => $columns); + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataBuilderTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataBuilderTest.php new file mode 100644 index 000000000..551f3bb78 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataBuilderTest.php @@ -0,0 +1,99 @@ +. + */ + + +namespace Doctrine\Tests\ORM\Mapping; + +use Doctrine\ORM\Mapping\ClassMetadata; +use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; + +/** + * @group DDC-659 + */ +class ClassMetadataBuilderTest extends \Doctrine\Tests\OrmTestCase +{ + /** + * @var ClassMetadata + */ + private $cm; + /** + * @var ClassMetadataBuilder + */ + private $builder; + + public function setUp() + { + $this->cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'); + $this->builder = new ClassMetadataBuilder($this->cm); + } + + public function testSetMappedSuperClass() + { + $this->builder->setMappedSuperClass(); + + $this->assertTrue($this->cm->isMappedSuperclass); + } + + public function testSetCustomRepositoryClass() + { + $this->builder->setCustomRepositoryClass('Doctrine\Tests\Models\CMS\CmsGroup'); + + $this->assertEquals('Doctrine\Tests\Models\CMS\CmsGroup', $this->cm->customRepositoryClassName); + } + + public function testSetReadOnly() + { + $this->builder->setReadOnly(); + $this->assertTrue($this->cm->isReadOnly); + } + + public function testSetTable() + { + $this->builder->setTable('users'); + $this->assertEquals('users', $this->cm->table['name']); + } + + public function testAddIndex() + { + $this->builder->addIndex(array('username', 'name'), 'users_idx'); + $this->assertEquals(array('users_idx' => array('columns' => array('username', 'name'))), $this->cm->table['indexes']); + } + + public function testAddUniqueConstraint() + { + $this->builder->addUniqueConstraint(array('username', 'name'), 'users_idx'); + $this->assertEquals(array('users_idx' => array('columns' => array('username', 'name'))), $this->cm->table['uniqueConstraints']); + } + + public function testSetPrimaryTableRelated() + { + $this->builder->addUniqueConstraint(array('username', 'name'), 'users_idx'); + $this->builder->addIndex(array('username', 'name'), 'users_idx'); + $this->builder->setTable('users'); + + $this->assertEquals( + array( + 'name' => 'users', + 'indexes' => array('users_idx' => array('columns' => array('username', 'name'))), + 'uniqueConstraints' => array('users_idx' => array('columns' => array('username', 'name'))), + ), + $this->cm->table + ); + } +} \ No newline at end of file From 09ac3a9eb245144cb44c4f34fbe7242ee2cced57 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 4 Sep 2011 14:13:20 +0200 Subject: [PATCH 3/3] DDC-659 - Add ClassMetadataBuilder for PHP based metadata management --- .../Mapping/Builder/AssociationBuilder.php | 167 +++++++++ .../Mapping/Builder/ClassMetadataBuilder.php | 332 ++++++++++++++++- .../ORM/Mapping/Builder/FieldBuilder.php | 223 +++++++++++ .../Builder/ManyToManyAssociationBuilder.php | 86 +++++ .../Builder/OneToManyAssociationBuilder.php | 62 ++++ lib/Doctrine/ORM/Mapping/ClassMetadata.php | 13 + .../ORM/Mapping/ClassMetadataInfo.php | 44 ++- lib/Doctrine/ORM/Mapping/MappingException.php | 5 + .../ORM/Mapping/ClassMetadataBuilderTest.php | 345 +++++++++++++++++- .../Tests/ORM/Mapping/ClassMetadataTest.php | 11 + 10 files changed, 1259 insertions(+), 29 deletions(-) create mode 100644 lib/Doctrine/ORM/Mapping/Builder/AssociationBuilder.php create mode 100644 lib/Doctrine/ORM/Mapping/Builder/FieldBuilder.php create mode 100644 lib/Doctrine/ORM/Mapping/Builder/ManyToManyAssociationBuilder.php create mode 100644 lib/Doctrine/ORM/Mapping/Builder/OneToManyAssociationBuilder.php diff --git a/lib/Doctrine/ORM/Mapping/Builder/AssociationBuilder.php b/lib/Doctrine/ORM/Mapping/Builder/AssociationBuilder.php new file mode 100644 index 000000000..cff4e5d52 --- /dev/null +++ b/lib/Doctrine/ORM/Mapping/Builder/AssociationBuilder.php @@ -0,0 +1,167 @@ +. + */ + + +namespace Doctrine\ORM\Mapping\Builder; + +use Doctrine\ORM\Mapping\ClassMetadata; + +class AssociationBuilder +{ + /** + * @var ClassMetadataBuilder + */ + protected $builder; + + /** + * @var array + */ + protected $mapping; + + /** + * @var array + */ + protected $joinColumns; + + /** + * + * @var int + */ + protected $type; + + /** + * @param ClassMetadataBuilder $builder + * @param array $mapping + */ + public function __construct(ClassMetadataBuilder $builder, array $mapping, $type) + { + $this->builder = $builder; + $this->mapping = $mapping; + $this->type = $type; + } + + public function mappedBy($fieldName) + { + $this->mapping['mappedBy'] = $fieldName; + return $this; + } + + public function inversedBy($fieldName) + { + $this->mapping['inversedBy'] = $fieldName; + return $this; + } + + public function cascadeAll() + { + $this->mapping['cascade'] = array("ALL"); + return $this; + } + + public function cascadePersist() + { + $this->mapping['cascade'][] = "persist"; + return $this; + } + + public function cascadeRemove() + { + $this->mapping['cascade'][] = "remove"; + return $this; + } + + public function cascadeMerge() + { + $this->mapping['cascade'][] = "merge"; + return $this; + } + + public function cascadeDetach() + { + $this->mapping['cascade'][] = "detach"; + return $this; + } + + public function cascadeRefresh() + { + $this->mapping['cascade'][] = "refresh"; + return $this; + } + + public function fetchExtraLazy() + { + $this->mapping['fetch'] = ClassMetadata::FETCH_EXTRA_LAZY; + return $this; + } + + public function fetchEager() + { + $this->mapping['fetch'] = ClassMetadata::FETCH_EAGER; + return $this; + } + + public function fetchLazy() + { + $this->mapping['fetch'] = ClassMetadata::FETCH_LAZY; + return $this; + } + + /** + * Add Join Columns + * + * @param string $columnName + * @param string $referencedColumnName + * @param bool $nullable + * @param bool $unique + * @param string $onDelete + * @param string $columnDef + */ + public function addJoinColumn($columnName, $referencedColumnName, $nullable = true, $unique = false, $onDelete = null, $columnDef = null) + { + $this->joinColumns[] = array( + 'name' => $columnName, + 'referencedColumnName' => $referencedColumnName, + 'nullable' => $nullable, + 'unique' => $unique, + 'onDelete' => $onDelete, + 'columnDefinition' => $columnDef, + ); + return $this; + } + + /** + * @return ClassMetadataBuilder + */ + public function build() + { + $mapping = $this->mapping; + if ($this->joinColumns) { + $mapping['joinColumns'] = $this->joinColumns; + } + $cm = $this->builder->getClassMetadata(); + if ($this->type == ClassMetadata::MANY_TO_ONE) { + $cm->mapManyToOne($mapping); + } else if ($this->type == ClassMetadata::ONE_TO_ONE) { + $cm->mapOneToOne($mapping); + } else { + throw new \InvalidArgumentException("Type should be a ToOne Assocation here"); + } + return $this->builder; + } +} \ No newline at end of file diff --git a/lib/Doctrine/ORM/Mapping/Builder/ClassMetadataBuilder.php b/lib/Doctrine/ORM/Mapping/Builder/ClassMetadataBuilder.php index b2a97e7f8..978a87601 100644 --- a/lib/Doctrine/ORM/Mapping/Builder/ClassMetadataBuilder.php +++ b/lib/Doctrine/ORM/Mapping/Builder/ClassMetadataBuilder.php @@ -26,7 +26,7 @@ use Doctrine\ORM\Mapping\ClassMetadata; * * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link www.doctrine-project.com - * @since 2.0 + * @since 2.2 * @author Benjamin Eberlei */ class ClassMetadataBuilder @@ -36,44 +36,372 @@ class ClassMetadataBuilder */ private $cm; + /** + * @param ClassMetadata $cm + */ public function __construct(ClassMetadata $cm) { $this->cm = $cm; } + /** + * @return ClassMetadata + */ + public function getClassMetadata() + { + return $this->cm; + } + + /** + * Mark the class as mapped superclass. + * + * @return ClassMetadataBuilder + */ public function setMappedSuperClass() { $this->cm->isMappedSuperclass = true; + return $this; } + /** + * Set custom Repository class name + * + * @param string $repositoryClassName + * @return ClassMetadataBuilder + */ public function setCustomRepositoryClass($repositoryClassName) { $this->cm->setCustomRepositoryClass($repositoryClassName); + return $this; } + /** + * Mark class read only + * + * @return ClassMetadataBuilder + */ public function setReadOnly() { $this->cm->markReadOnly(); + return $this; } + /** + * Set the table name + * + * @param string $name + * @return ClassMetadataBuilder + */ public function setTable($name) { $this->cm->setPrimaryTable(array('name' => $name)); + return $this; } + /** + * Add Index + * + * @param array $columns + * @param string $name + * @return ClassMetadataBuilder + */ public function addIndex(array $columns, $name) { if (!isset($this->cm->table['indexes'])) { $this->cm->table['indexes'] = array(); } $this->cm->table['indexes'][$name] = array('columns' => $columns); + return $this; } + /** + * Add Unique Constraint + * + * @param array $columns + * @param string $name + * @return ClassMetadataBuilder + */ public function addUniqueConstraint(array $columns, $name) { if (!isset($this->cm->table['uniqueConstraints'])) { $this->cm->table['uniqueConstraints'] = array(); } $this->cm->table['uniqueConstraints'][$name] = array('columns' => $columns); + return $this; } -} \ No newline at end of file + + /** + * Add named query + * + * @param string $name + * @param string $dqlQuery + * @return ClassMetadataBuilder + */ + public function addNamedQuery($name, $dqlQuery) + { + $this->cm->addNamedQuery(array( + 'name' => $name, + 'query' => $dqlQuery, + )); + return $this; + } + + /** + * Set class as root of a joined table inheritance hierachy. + * + * @return ClassMetadataBuilder + */ + public function setJoinedTableInheritance() + { + $this->cm->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_JOINED); + return $this; + } + + /** + * Set class as root of a single table inheritance hierachy. + * + * @return ClassMetadataBuilder + */ + public function setSingleTableInheritance() + { + $this->cm->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE); + return $this; + } + + /** + * Set the discriminator column details. + * + * @param string $name + * @param string $type + */ + public function setDiscriminatorColumn($name, $type = 'string', $length = 255) + { + $this->cm->setDiscriminatorColumn(array( + 'name' => $name, + 'type' => $type, + 'length' => $length, + )); + return $this; + } + + /** + * Add a subclass to this inheritance hierachy. + * + * @param string $name + * @param string $class + * @return ClassMetadataBuilder + */ + public function addDiscriminatorMapClass($name, $class) + { + $this->cm->addDiscriminatorMapClass($name, $class); + return $this; + } + + /** + * Set deferred explicit change tracking policy. + * + * @return ClassMetadataBuilder + */ + public function setChangeTrackingPolicyDeferredExplicit() + { + $this->cm->setChangeTrackingPolicy(ClassMetadata::CHANGETRACKING_DEFERRED_EXPLICIT); + return $this; + } + + /** + * Set notify change tracking policy. + * + * @return ClassMetadataBuilder + */ + public function setChangeTrackingPolicyNotify() + { + $this->cm->setChangeTrackingPolicy(ClassMetadata::CHANGETRACKING_NOTIFY); + return $this; + } + + /** + * Add lifecycle event + * + * @param string $methodName + * @param string $event + * @return ClassMetadataBuilder + */ + public function addLifecycleEvent($methodName, $event) + { + $this->cm->addLifecycleCallback($methodName, $event); + return $this; + } + + /** + * Add Field + * + * @param string $name + * @param string $type + * @param array $mapping + */ + public function addField($name, $type, array $mapping = array()) + { + $mapping['fieldName'] = $name; + $mapping['type'] = $type; + $this->cm->mapField($mapping); + return $this; + } + + /** + * Create a field builder. + * + * @param string $name + * @param string $type + * @return FieldBuilder + */ + public function createField($name, $type) + { + return new FieldBuilder($this, array('fieldName' => $name, 'type' => $type)); + } + + /** + * Add a simple many to one association, optionally with the inversed by field. + * + * @param string $name + * @param string $targetEntity + * @param string|null $inversedBy + * @return ClassMetadataBuilder + */ + public function addManyToOne($name, $targetEntity, $inversedBy = null) + { + $builder = $this->createManyToOne($name, $targetEntity); + if ($inversedBy) { + $builder->setInversedBy($inversedBy); + } + return $builder->build(); + } + + /** + * Create a ManyToOne Assocation Builder. + * + * Note: This method does not add the association, you have to call build() on the AssociationBuilder. + * + * @param string $name + * @param string $targetEntity + * @return AssociationBuilder + */ + public function createManyToOne($name, $targetEntity) + { + return new AssociationBuilder($this, array('fieldName' => $name, 'targetEntity' => $targetEntity), ClassMetadata::MANY_TO_ONE); + } + + /** + * Create OneToOne Assocation Builder + * + * @param string $name + * @param string $targetEntity + * @return AssociationBuilder + */ + public function createOneToOne($name, $targetEntity) + { + return new AssociationBuilder($this, array('fieldName' => $name, 'targetEntity' => $targetEntity), ClassMetadata::ONE_TO_ONE); + } + + /** + * Add simple inverse one-to-one assocation. + * + * @param string $name + * @param string $targetEntity + * @param string $mappedBy + * @return ClassMetadataBuilder + */ + public function addInverseOneToOne($name, $targetEntity, $mappedBy) + { + $builder = $this->createOneToOne($name, $targetEntity); + $builder->setMappedBy($mappedBy); + return $builder->build(); + } + + /** + * Add simple owning one-to-one assocation. + * + * @param string $name + * @param string $targetEntity + * @param string $inversedBy + * @return ClassMetadataBuilder + */ + public function addOwningOneToOne($name, $targetEntity, $inversedBy = null) + { + $builder = $this->createOneToOne($name, $targetEntity); + if ($inversedBy) { + $builder->setInversedBy($inversedBy); + } + return $builder->build(); + } + + /** + * Create ManyToMany Assocation Builder + * + * @param string $name + * @param string $targetEntity + * @return ManyToManyAssociationBuilder + */ + public function createManyToMany($name, $targetEntity) + { + return new ManyToManyAssociationBuilder($this, array('fieldName' => $name, 'targetEntity' => $targetEntity), ClassMetadata::MANY_TO_MANY); + } + + /** + * Add a simple owning many to many assocation. + * + * @param string $name + * @param string $targetEntity + * @param string|null $inversedBy + * @return ClassMetadataBuilder + */ + public function addOwningManyToMany($name, $targetEntity, $inversedBy = null) + { + $builder = $this->createManyToMany($name, $targetEntity); + if ($inversedBy) { + $builder->setInversedBy($inversedBy); + } + return $builder->build(); + } + + /** + * Add a simple inverse many to many assocation. + * + * @param string $name + * @param string $targetEntity + * @param string $mappedBy + * @return ClassMetadataBuilder + */ + public function addInverseManyToMany($name, $targetEntity, $mappedBy) + { + $builder = $this->createManyToMany($name, $targetEntity); + $builder->setMappedBy($mappedBy); + return $builder->build(); + } + + /** + * Create a one to many assocation builder + * + * @param string $name + * @param string $targetEntity + * @return OneToManyAssociationBuilder + */ + public function createOneToMany($name, $targetEntity) + { + return new OneToManyAssociationBuilder($this, array('fieldName' => $name, 'targetEntity' => $targetEntity), ClassMetadata::ONE_TO_MANY); + } + + /** + * Add simple OneToMany assocation. + * + * @param string $name + * @param string $targetEntity + * @param string $mappedBy + * @return ClassMetadataBuilder + */ + public function addOneToMany($name, $targetEntity, $mappedBy) + { + $builder = $this->createOneToMany($name, $targetEntity); + $builder->setMappedBy($mappedBy); + return $builder->build(); + } +} diff --git a/lib/Doctrine/ORM/Mapping/Builder/FieldBuilder.php b/lib/Doctrine/ORM/Mapping/Builder/FieldBuilder.php new file mode 100644 index 000000000..9278a6db0 --- /dev/null +++ b/lib/Doctrine/ORM/Mapping/Builder/FieldBuilder.php @@ -0,0 +1,223 @@ +. + */ + + +namespace Doctrine\ORM\Mapping\Builder; + +/** + * Field Builder + * + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.doctrine-project.com + * @since 2.2 + * @author Benjamin Eberlei + */ +class FieldBuilder +{ + /** + * @var ClassMetadataBuilder + */ + private $builder; + /** + * @var array + */ + private $mapping; + /** + * @var bool + */ + private $version; + + /** + * @var string + */ + private $generatedValue; + + /** + * @var array + */ + private $sequenceDef; + + /** + * + * @param ClassMetadataBuilder $builder + * @param array $mapping + */ + public function __construct(ClassMetadataBuilder $builder, array $mapping) + { + $this->builder = $builder; + $this->mapping = $mapping; + } + + /** + * Set length. + * + * @param int $length + * @return FieldBuilder + */ + public function length($length) + { + $this->mapping['length'] = $length; + return $this; + } + + /** + * Set nullable + * + * @param bool + * @return FieldBuilder + */ + public function nullable($flag = true) + { + $this->mapping['nullable'] = (bool)$flag; + return $this; + } + + /** + * Set Unique + * + * @param bool + * @return FieldBuilder + */ + public function unique($flag = true) + { + $this->mapping['unique'] = (bool)$flag; + return $this; + } + + /** + * Set column name + * + * @param string $name + * @return FieldBuilder + */ + public function columnName($name) + { + $this->mapping['columnName'] = $name; + return $this; + } + + /** + * Set Precision + * + * @param int $p + * @return FieldBuilder + */ + public function precision($p) + { + $this->mapping['precision'] = $p; + return $this; + } + + /** + * Set scale. + * + * @param int $s + * @return FieldBuilder + */ + public function scale($s) + { + $this->mapping['scale'] = $s; + return $this; + } + + /** + * Set field as primary key. + * + * @return FieldBuilder + */ + public function isPrimaryKey() + { + $this->mapping['id'] = true; + return $this; + } + + /** + * @param int $strategy + * @return FieldBuilder + */ + public function generatedValue($strategy = 'AUTO') + { + $this->generatedValue = $strategy; + return $this; + } + + /** + * Set field versioned + * + * @return FieldBuilder + */ + public function isVersionField() + { + $this->version = true; + return $this; + } + + /** + * Set Sequence Generator + * + * @param string $sequenceName + * @param int $allocationSize + * @param int $initialValue + * @return FieldBuilder + */ + public function setSequenceGenerator($sequenceName, $allocationSize = 1, $initialValue = 1) + { + $this->sequenceDef = array( + 'sequenceName' => $sequenceName, + 'allocationSize' => $allocationSize, + 'initialValue' => $initialValue, + ); + return $this; + } + + /** + * Set column definition. + * + * @param string $def + * @return FieldBuilder + */ + public function columnDefinition($def) + { + $this->mapping['columnDefinition'] = $def; + return $this; + } + + /** + * Finalize this field and attach it to the ClassMetadata. + * + * Without this call a FieldBuilder has no effect on the ClassMetadata. + * + * @return ClassMetadataBuilder + */ + public function build() + { + $cm = $this->builder->getClassMetadata(); + if ($this->generatedValue) { + $cm->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_' . $this->generatedValue)); + } + if ($this->version) { + $cm->setVersionMapping($this->mapping); + } + $cm->mapField($this->mapping); + if ($this->sequenceDef) { + $cm->setSequenceGeneratorDefinition($this->sequenceDef); + } + return $this->builder; + } +} \ No newline at end of file diff --git a/lib/Doctrine/ORM/Mapping/Builder/ManyToManyAssociationBuilder.php b/lib/Doctrine/ORM/Mapping/Builder/ManyToManyAssociationBuilder.php new file mode 100644 index 000000000..c1dd1e568 --- /dev/null +++ b/lib/Doctrine/ORM/Mapping/Builder/ManyToManyAssociationBuilder.php @@ -0,0 +1,86 @@ +. + */ + + +namespace Doctrine\ORM\Mapping\Builder; + +/** + * ManyToMany Association Builder + * + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.doctrine-project.com + * @since 2.0 + * @author Benjamin Eberlei + */ +class ManyToManyAssociationBuilder extends OneToManyAssociationBuilder +{ + private $joinTableName; + + private $inverseJoinColumns = array(); + + public function setJoinTable($name) + { + $this->joinTableName = $name; + return $this; + } + + /** + * Add Inverse Join Columns + * + * @param string $columnName + * @param string $referencedColumnName + * @param bool $nullable + * @param bool $unique + * @param string $onDelete + * @param string $columnDef + */ + public function addInverseJoinColumn($columnName, $referencedColumnName, $nullable = true, $unique = false, $onDelete = null, $columnDef = null) + { + $this->inverseJoinColumns[] = array( + 'name' => $columnName, + 'referencedColumnName' => $referencedColumnName, + 'nullable' => $nullable, + 'unique' => $unique, + 'onDelete' => $onDelete, + 'columnDefinition' => $columnDef, + ); + return $this; + } + + /** + * @return ClassMetadataBuilder + */ + public function build() + { + $mapping = $this->mapping; + $mapping['joinTable'] = array(); + if ($this->joinColumns) { + $mapping['joinTable']['joinColumns'] = $this->joinColumns; + } + if ($this->inverseJoinColumns) { + $mapping['joinTable']['inverseJoinColumns'] = $this->inverseJoinColumns; + } + if ($this->joinTableName) { + $mapping['joinTable']['name'] = $this->joinTableName; + } + $cm = $this->builder->getClassMetadata(); + $cm->mapManyToMany($mapping); + return $this->builder; + } +} \ No newline at end of file diff --git a/lib/Doctrine/ORM/Mapping/Builder/OneToManyAssociationBuilder.php b/lib/Doctrine/ORM/Mapping/Builder/OneToManyAssociationBuilder.php new file mode 100644 index 000000000..be55c2d6b --- /dev/null +++ b/lib/Doctrine/ORM/Mapping/Builder/OneToManyAssociationBuilder.php @@ -0,0 +1,62 @@ +. + */ + + +namespace Doctrine\ORM\Mapping\Builder; + +/** + * OneToMany Association Builder + * + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.doctrine-project.com + * @since 2.0 + * @author Benjamin Eberlei + */ +class OneToManyAssociationBuilder extends AssociationBuilder +{ + /** + * @param array $fieldNames + * @return OneToManyAssociationBuilder + */ + public function setOrderBy(array $fieldNames) + { + $this->mapping['orderBy'] = $fieldNames; + return $this; + } + + public function setIndexBy($fieldName) + { + $this->mapping['indexBy'] = $fieldName; + return $this; + } + + /** + * @return ClassMetadataBuilder + */ + public function build() + { + $mapping = $this->mapping; + if ($this->joinColumns) { + $mapping['joinColumns'] = $this->joinColumns; + } + $cm = $this->builder->getClassMetadata(); + $cm->mapOneToMany($mapping); + return $this->builder; + } +} \ No newline at end of file diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadata.php b/lib/Doctrine/ORM/Mapping/ClassMetadata.php index bbabc2fe0..9caa79e11 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadata.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadata.php @@ -343,4 +343,17 @@ class ClassMetadata extends ClassMetadataInfo } return clone $this->_prototype; } + + /** + * @param string $callback + * @param string $event + */ + public function addLifecycleCallback($callback, $event) + { + if ( !$this->reflClass->hasMethod($callback) || + ($this->reflClass->getMethod($callback)->getModifiers() & \ReflectionMethod::IS_PUBLIC) == 0) { + throw MappingException::lifecycleCallbackMethodNotFound($this->name, $callback); + } + return parent::addLifecycleCallback($callback, $event); + } } diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index aa6e038be..d791ce0dd 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -1568,9 +1568,6 @@ class ClassMetadataInfo implements ClassMetadata /** * Adds a lifecycle callback for entities of this class. * - * Note: If the same callback is registered more than once, the old one - * will be overridden. - * * @param string $callback * @param string $event */ @@ -1629,22 +1626,33 @@ class ClassMetadataInfo implements ClassMetadata public function setDiscriminatorMap(array $map) { foreach ($map as $value => $className) { - if (strlen($this->namespace) > 0 && strpos($className, '\\') === false) { - $className = $this->namespace . '\\' . $className; + $this->addDiscriminatorMapClass($value, $className); + } + } + + /** + * Add one entry of the discriminator map with a new class and corresponding name. + * + * @param string $name + * @param string $className + */ + public function addDiscriminatorMapClass($name, $className) + { + if (strlen($this->namespace) > 0 && strpos($className, '\\') === false) { + $className = $this->namespace . '\\' . $className; + } + + $className = ltrim($className, '\\'); + $this->discriminatorMap[$name] = $className; + + if ($this->name == $className) { + $this->discriminatorValue = $name; + } else { + if ( ! class_exists($className)) { + throw MappingException::invalidClassInDiscriminatorMap($className, $this->name); } - - $className = ltrim($className, '\\'); - $this->discriminatorMap[$value] = $className; - - if ($this->name == $className) { - $this->discriminatorValue = $value; - } else { - if ( ! class_exists($className)) { - throw MappingException::invalidClassInDiscriminatorMap($className, $this->name); - } - if (is_subclass_of($className, $this->name)) { - $this->subClasses[] = $className; - } + if (is_subclass_of($className, $this->name)) { + $this->subClasses[] = $className; } } } diff --git a/lib/Doctrine/ORM/Mapping/MappingException.php b/lib/Doctrine/ORM/Mapping/MappingException.php index 5bedf3a06..97fbf94f6 100644 --- a/lib/Doctrine/ORM/Mapping/MappingException.php +++ b/lib/Doctrine/ORM/Mapping/MappingException.php @@ -293,4 +293,9 @@ class MappingException extends \Doctrine\ORM\ORMException "to avoid this exception from occuring." ); } + + public static function lifecycleCallbackMethodNotFound($className, $methodName) + { + return new self("Entity '" . $className . "' has no method '" . $methodName . "' to be registered as lifecycle callback."); + } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataBuilderTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataBuilderTest.php index 551f3bb78..38cecd5cc 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataBuilderTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataBuilderTest.php @@ -45,39 +45,37 @@ class ClassMetadataBuilderTest extends \Doctrine\Tests\OrmTestCase public function testSetMappedSuperClass() { - $this->builder->setMappedSuperClass(); - + $this->assertIsFluent($this->builder->setMappedSuperClass()); $this->assertTrue($this->cm->isMappedSuperclass); } public function testSetCustomRepositoryClass() { - $this->builder->setCustomRepositoryClass('Doctrine\Tests\Models\CMS\CmsGroup'); - + $this->assertIsFluent($this->builder->setCustomRepositoryClass('Doctrine\Tests\Models\CMS\CmsGroup')); $this->assertEquals('Doctrine\Tests\Models\CMS\CmsGroup', $this->cm->customRepositoryClassName); } public function testSetReadOnly() { - $this->builder->setReadOnly(); + $this->assertIsFluent($this->builder->setReadOnly()); $this->assertTrue($this->cm->isReadOnly); } public function testSetTable() { - $this->builder->setTable('users'); + $this->assertIsFluent($this->builder->setTable('users')); $this->assertEquals('users', $this->cm->table['name']); } public function testAddIndex() { - $this->builder->addIndex(array('username', 'name'), 'users_idx'); + $this->assertIsFluent($this->builder->addIndex(array('username', 'name'), 'users_idx')); $this->assertEquals(array('users_idx' => array('columns' => array('username', 'name'))), $this->cm->table['indexes']); } public function testAddUniqueConstraint() { - $this->builder->addUniqueConstraint(array('username', 'name'), 'users_idx'); + $this->assertIsFluent($this->builder->addUniqueConstraint(array('username', 'name'), 'users_idx')); $this->assertEquals(array('users_idx' => array('columns' => array('username', 'name'))), $this->cm->table['uniqueConstraints']); } @@ -96,4 +94,333 @@ class ClassMetadataBuilderTest extends \Doctrine\Tests\OrmTestCase $this->cm->table ); } -} \ No newline at end of file + + public function testSetInheritanceJoined() + { + $this->assertIsFluent($this->builder->setJoinedTableInheritance()); + $this->assertEquals(ClassMetadata::INHERITANCE_TYPE_JOINED, $this->cm->inheritanceType); + } + + public function testSetInheritanceSingleTable() + { + $this->assertIsFluent($this->builder->setSingleTableInheritance()); + $this->assertEquals(ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE, $this->cm->inheritanceType); + } + + public function testSetDiscriminatorColumn() + { + $this->assertIsFluent($this->builder->setDiscriminatorColumn('discr', 'string', '124')); + $this->assertEquals(array('fieldName' => 'discr', 'name' => 'discr', 'type' => 'string', 'length' => '124'), $this->cm->discriminatorColumn); + } + + public function testAddDiscriminatorMapClass() + { + $this->assertIsFluent($this->builder->addDiscriminatorMapClass('test', 'Doctrine\Tests\Models\CMS\CmsUser')); + $this->assertIsFluent($this->builder->addDiscriminatorMapClass('test2', 'Doctrine\Tests\Models\CMS\CmsGroup')); + + $this->assertEquals(array('test' => 'Doctrine\Tests\Models\CMS\CmsUser', 'test2' => 'Doctrine\Tests\Models\CMS\CmsGroup'), $this->cm->discriminatorMap); + $this->assertEquals('test', $this->cm->discriminatorValue); + } + + public function testChangeTrackingPolicyExplicit() + { + $this->assertIsFluent($this->builder->setChangeTrackingPolicyDeferredExplicit()); + $this->assertEquals(ClassMetadata::CHANGETRACKING_DEFERRED_EXPLICIT, $this->cm->changeTrackingPolicy); + } + + public function testChangeTrackingPolicyNotify() + { + $this->assertIsFluent($this->builder->setChangeTrackingPolicyNotify()); + $this->assertEquals(ClassMetadata::CHANGETRACKING_NOTIFY, $this->cm->changeTrackingPolicy); + } + + public function testAddField() + { + $this->assertIsFluent($this->builder->addField('name', 'string')); + $this->assertEquals(array('columnName' => 'name', 'fieldName' => 'name', 'type' => 'string'), $this->cm->fieldMappings['name']); + } + + public function testCreateField() + { + $fieldBuilder = ($this->builder->createField('name', 'string')); + $this->assertInstanceOf('Doctrine\ORM\Mapping\Builder\FieldBuilder', $fieldBuilder); + + $this->assertFalse(isset($this->cm->fieldMappings['name'])); + $this->assertIsFluent($fieldBuilder->build()); + $this->assertEquals(array('columnName' => 'name', 'fieldName' => 'name', 'type' => 'string'), $this->cm->fieldMappings['name']); + } + + public function testCreateVersionedField() + { + $this->builder->createField('name', 'integer')->columnName('username')->length(124)->nullable()->columnDefinition('foobar')->unique()->isVersionField()->build(); + $this->assertEquals(array( + 'columnDefinition' => 'foobar', + 'columnName' => 'username', + 'default' => 1, + 'fieldName' => 'name', + 'length' => 124, + 'type' => 'integer', + 'nullable' => true, + 'unique' => true, + ), $this->cm->fieldMappings['name']); + } + + public function testCreatePrimaryField() + { + $this->builder->createField('id', 'integer')->isPrimaryKey()->generatedValue()->build(); + + $this->assertEquals(array('id'), $this->cm->identifier); + $this->assertEquals(array('columnName' => 'id', 'fieldName' => 'id', 'id' => true, 'type' => 'integer'), $this->cm->fieldMappings['id']); + } + + public function testAddLifecycleEvent() + { + $this->builder->addLifecycleEvent('getStatus', 'postLoad'); + + $this->assertEquals(array('postLoad' => array('getStatus')), $this->cm->lifecycleCallbacks); + } + + public function testCreateManyToOne() + { + $this->assertIsFluent( + $this->builder->createManyToOne('groups', 'Doctrine\Tests\Models\CMS\CmsGroup') + ->addJoinColumn('group_id', 'id', true, false, 'CASCADE') + ->cascadeAll() + ->fetchExtraLazy() + ->build() + ); + + $this->assertEquals(array('groups' => array ( + 'fieldName' => 'groups', + 'targetEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsGroup', + 'cascade' => array ( + 0 => 'remove', + 1 => 'persist', + 2 => 'refresh', + 3 => 'merge', + 4 => 'detach', + ), + 'fetch' => 4, + 'joinColumns' => array ( + 0 => + array ( + 'name' => 'group_id', + 'referencedColumnName' => 'id', + 'nullable' => true, + 'unique' => false, + 'onDelete' => 'CASCADE', + 'columnDefinition' => NULL, + ), + ), + 'type' => 2, + 'mappedBy' => NULL, + 'inversedBy' => NULL, + 'isOwningSide' => true, + 'sourceEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsUser', + 'isCascadeRemove' => true, + 'isCascadePersist' => true, + 'isCascadeRefresh' => true, + 'isCascadeMerge' => true, + 'isCascadeDetach' => true, + 'sourceToTargetKeyColumns' => + array ( + 'group_id' => 'id', + ), + 'joinColumnFieldNames' => + array ( + 'group_id' => 'group_id', + ), + 'targetToSourceKeyColumns' => + array ( + 'id' => 'group_id', + ), + 'orphanRemoval' => false, + ), + ), $this->cm->associationMappings); + } + + public function testCreateOneToOne() + { + $this->assertIsFluent( + $this->builder->createOneToOne('groups', 'Doctrine\Tests\Models\CMS\CmsGroup') + ->addJoinColumn('group_id', 'id', true, false, 'CASCADE') + ->cascadeAll() + ->fetchExtraLazy() + ->build() + ); + + $this->assertEquals(array('groups' => array ( + 'fieldName' => 'groups', + 'targetEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsGroup', + 'cascade' => array ( + 0 => 'remove', + 1 => 'persist', + 2 => 'refresh', + 3 => 'merge', + 4 => 'detach', + ), + 'fetch' => 4, + 'joinColumns' => array ( + 0 => + array ( + 'name' => 'group_id', + 'referencedColumnName' => 'id', + 'nullable' => true, + 'unique' => true, + 'onDelete' => 'CASCADE', + 'columnDefinition' => NULL, + ), + ), + 'type' => 1, + 'mappedBy' => NULL, + 'inversedBy' => NULL, + 'isOwningSide' => true, + 'sourceEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsUser', + 'isCascadeRemove' => true, + 'isCascadePersist' => true, + 'isCascadeRefresh' => true, + 'isCascadeMerge' => true, + 'isCascadeDetach' => true, + 'sourceToTargetKeyColumns' => + array ( + 'group_id' => 'id', + ), + 'joinColumnFieldNames' => + array ( + 'group_id' => 'group_id', + ), + 'targetToSourceKeyColumns' => + array ( + 'id' => 'group_id', + ), + 'orphanRemoval' => false, + ), + ), $this->cm->associationMappings); + } + + public function testCreateManyToMany() + { + $this->assertIsFluent( + $this->builder->createManyToMany('groups', 'Doctrine\Tests\Models\CMS\CmsGroup') + ->setJoinTable('groups_users') + ->addJoinColumn('group_id', 'id', true, false, 'CASCADE') + ->addInverseJoinColumn('user_id', 'id') + ->cascadeAll() + ->fetchExtraLazy() + ->build() + ); + + $this->assertEquals(array( + 'groups' => + array( + 'fieldName' => 'groups', + 'targetEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsGroup', + 'cascade' => + array( + 0 => 'remove', + 1 => 'persist', + 2 => 'refresh', + 3 => 'merge', + 4 => 'detach', + ), + 'fetch' => 4, + 'joinTable' => + array( + 'joinColumns' => + array( + 0 => + array( + 'name' => 'group_id', + 'referencedColumnName' => 'id', + 'nullable' => true, + 'unique' => false, + 'onDelete' => 'CASCADE', + 'columnDefinition' => NULL, + ), + ), + 'inverseJoinColumns' => + array( + 0 => + array( + 'name' => 'user_id', + 'referencedColumnName' => 'id', + 'nullable' => true, + 'unique' => false, + 'onDelete' => NULL, + 'columnDefinition' => NULL, + ), + ), + 'name' => 'groups_users', + ), + 'type' => 8, + 'mappedBy' => NULL, + 'inversedBy' => NULL, + 'isOwningSide' => true, + 'sourceEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsUser', + 'isCascadeRemove' => true, + 'isCascadePersist' => true, + 'isCascadeRefresh' => true, + 'isCascadeMerge' => true, + 'isCascadeDetach' => true, + 'isOnDeleteCascade' => true, + 'relationToSourceKeyColumns' => + array( + 'group_id' => 'id', + ), + 'joinTableColumns' => + array( + 0 => 'group_id', + 1 => 'user_id', + ), + 'relationToTargetKeyColumns' => + array( + 'user_id' => 'id', + ), + ), + ), $this->cm->associationMappings); + } + + public function testCreateOneToMany() + { + $this->assertIsFluent( + $this->builder->createOneToMany('groups', 'Doctrine\Tests\Models\CMS\CmsGroup') + ->mappedBy('test') + ->setOrderBy(array('test')) + ->setIndexBy('test') + ->build() + ); + + $this->assertEquals(array( + 'groups' => + array( + 'fieldName' => 'groups', + 'targetEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsGroup', + 'mappedBy' => 'test', + 'orderBy' => + array( + 0 => 'test', + ), + 'indexBy' => 'test', + 'type' => 4, + 'inversedBy' => NULL, + 'isOwningSide' => false, + 'sourceEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsUser', + 'fetch' => 2, + 'cascade' => + array( + ), + 'isCascadeRemove' => false, + 'isCascadePersist' => false, + 'isCascadeRefresh' => false, + 'isCascadeMerge' => false, + 'isCascadeDetach' => false, + 'orphanRemoval' => false, + ), + ), $this->cm->associationMappings); + } + + public function assertIsFluent($ret) + { + $this->assertSame($this->builder, $ret, "Return Value has to be same instance as used builder"); + } +} diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php index d16ec19f0..fed31d9c5 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php @@ -471,4 +471,15 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase $cm = new ClassMetadata('DOCTRINE\TESTS\MODELS\CMS\CMSUSER'); $this->assertEquals('Doctrine\Tests\Models\CMS\CmsUser', $cm->name); } + + /** + * @group DDC-659 + */ + public function testLifecycleCallbackNotFound() + { + $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'); + + $this->setExpectedException("Doctrine\ORM\Mapping\MappingException", "Entity 'Doctrine\Tests\Models\CMS\CmsUser' has no method 'notfound' to be registered as lifecycle callback."); + $cm->addLifecycleCallback('notfound', 'postLoad'); + } }