From 48b6356e53d1611fd44c9274e0d843eefd29ffda Mon Sep 17 00:00:00 2001 From: Vitali Yakavenka Date: Sat, 26 Nov 2011 11:32:44 +0100 Subject: [PATCH] Add support for GenerateValue(strategy='CUSTOM') in AnnotationDriver --- .../ORM/Mapping/ClassMetadataInfo.php | 8 +++++ .../ORM/Mapping/Driver/AnnotationDriver.php | 5 +++ .../Mapping/Driver/DoctrineAnnotations.php | 3 +- .../ORM/Mapping/AnnotationDriverTest.php | 31 +++++++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index 2f78d18a1..99ce88968 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -1828,6 +1828,14 @@ class ClassMetadataInfo implements ClassMetadata { $this->idGenerator = $generator; } + + /** + * Sets definition + * @param array $definition + */ + public function setCustomGeneratorDefinition(array $definition) { + $this->customGeneratorDefinition = $definition; + } /** * Sets the definition of the sequence ID generator for this class. diff --git a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php index d469fd66e..71a46e169 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php @@ -319,6 +319,11 @@ class AnnotationDriver implements Driver )); } else if ($tblGeneratorAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\TableGenerator')) { throw MappingException::tableIdGeneratorNotImplemented($className); + } else if ($customGeneratorAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\CustomIdGenerator')) { + $metadata->setCustomGeneratorDefinition(array( + 'class' => $customGeneratorAnnot->class, + 'args' => $customGeneratorAnnot->args + )); } } else if ($oneToOneAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OneToOne')) { if ($idAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Id')) { diff --git a/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php b/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php index 290fc6529..5ed05530f 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php +++ b/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php @@ -39,6 +39,7 @@ require_once __DIR__.'/../UniqueConstraint.php'; require_once __DIR__.'/../Index.php'; require_once __DIR__.'/../JoinTable.php'; require_once __DIR__.'/../SequenceGenerator.php'; +require_once __DIR__.'/../CustomIdGenerator.php'; require_once __DIR__.'/../ChangeTrackingPolicy.php'; require_once __DIR__.'/../OrderBy.php'; require_once __DIR__.'/../NamedQueries.php'; @@ -51,4 +52,4 @@ require_once __DIR__.'/../PostUpdate.php'; require_once __DIR__.'/../PreRemove.php'; require_once __DIR__.'/../PostRemove.php'; require_once __DIR__.'/../PostLoad.php'; -require_once __DIR__.'/../PreFlush.php'; +require_once __DIR__.'/../PreFlush.php'; \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php index 9273f7c6b..a491fc326 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php @@ -4,6 +4,7 @@ namespace Doctrine\Tests\ORM\Mapping; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Events; +use Doctrine\Common\Annotations\AnnotationRegistry; require_once __DIR__ . '/../../TestInit.php'; @@ -21,6 +22,21 @@ class AnnotationDriverTest extends AbstractMappingDriverTest $this->setExpectedException('Doctrine\ORM\Mapping\MappingException'); $annotationDriver->loadMetadataForClass('stdClass', $cm); } + + public function testLoadMetadataForClassSetsCustomIdGenerator() + { + $cm = new ClassMetadata("Doctrine\Tests\ORM\Mapping\CustomIdGeneratorClass"); + $driver = $this->_loadDriver(); + $expected = array("class" => "\stdClass", + "args" => array("par1", "par2")); + + $driver->loadMetadataForClass( + "Docrtine\Tests\ORM\Mapping\CustomIdGeneratorClass", $cm); + + $this->assertEquals(ClassMetadata::GENERATOR_TYPE_CUSTOM, $cm->generatorType); + $this->assertEquals($expected, $cm->customGeneratorDefinition); + + } /** * @group DDC-268 @@ -99,6 +115,8 @@ class AnnotationDriverTest extends AbstractMappingDriverTest protected function _loadDriver() { + AnnotationRegistry::registerFile(__DIR__ . + '/../../../../../lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'); return $this->createAnnotationDriver(); } @@ -215,6 +233,19 @@ class AnnotationDriverTest extends AbstractMappingDriverTest } } +/** + * @Entity + */ +class CustomIdGeneratorClass +{ + /** + * @Id @Column + * @GeneratedValue(strategy="CUSTOM") + * @CustomIdGenerator(class="\stdClass", args={"par1", "par2"}) + */ + public $id; +} + /** * @Entity */