1
0
mirror of synced 2025-03-17 21:43:55 +03:00

Allow loading of custom ID generator class by FQN in @GeneratedValue(type=)

This commit is contained in:
Vitali 2011-11-20 16:15:40 +03:00 committed by Vitali Yakavenka
parent 3f942e05f3
commit ffc722a334
2 changed files with 51 additions and 2 deletions

View File

@ -505,9 +505,13 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface
throw new ORMException("TableGenerator not yet implemented.");
break;
default:
if (class_exists($class->generatorType)) {
$class->setIdGenerator(new $class->generatorType);
} else {
throw new ORMException("Unknown generator type: " . $class->generatorType);
}
}
}
/**
* Check if this class is mapped by this EntityManager + ClassMetadata configuration

View File

@ -64,10 +64,40 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase
$this->assertEquals(ClassMetadata::GENERATOR_TYPE_SEQUENCE, $cm1->generatorType);
}
public function testGetMetadataFor_ReturnLoadedCustomIdGenerator() {
$cm1 = new ClassMetadata('Doctrine\Tests\ORM\Mapping\TestEntity1');
$cm1->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
$cm1->setIdGeneratorType("Doctrine\Tests\ORM\Mapping\CustomIdGenerator");
$cmf = $this->_createTestFactory();
$cmf->setMetadataForClass('Doctrine\Tests\ORM\Mapping\TestEntity1', $cm1);
$actual = $cmf->getMetadataFor('Doctrine\Tests\ORM\Mapping\TestEntity1');
$this->assertEquals("Doctrine\Tests\ORM\Mapping\CustomIdGenerator",
$actual->generatorType);
$this->assertInstanceOf("Doctrine\Tests\ORM\Mapping\CustomIdGenerator",
$actual->idGenerator);
/**
* @GeneratedValue(type=@CustomIdGenerator(table="test"))
*/
}
public function testGetMetadataFor_ThrowsExceptionOnUnknownCustomGeneratorClass() {
$cm1 = new ClassMetadata('Doctrine\Tests\ORM\Mapping\TestEntity1');
$cm1->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
$cm1->setIdGeneratorType("NotExistingIdGenerator");
$cmf = $this->_createTestFactory();
$cmf->setMetadataForClass('Doctrine\Tests\ORM\Mapping\TestEntity1', $cm1);
$this->setExpectedException("Doctrine\ORM\ORMException");
$actual = $cmf->getMetadataFor('Doctrine\Tests\ORM\Mapping\TestEntity1');
}
public function testHasGetMetadata_NamespaceSeperatorIsNotNormalized()
{
require_once __DIR__."/../../Models/Global/GlobalNamespaceModel.php";
\Doctrine\Common\Annotations\AnnotationRegistry::registerFile(__DIR__ . '/../../../../../lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php');
AnnotationRegistry::registerFile(__DIR__ .
'/../../../../../lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php');
$metadataDriver = $this->createAnnotationDriver(array(__DIR__ . '/../../Models/Global/'));
@ -142,6 +172,17 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase
return EntityManagerMock::create($conn, $config, $eventManager);
}
/**
* @return ClassMetadataFactoryTestSubject
*/
protected function _createTestFactory() {
$mockDriver = new MetadataDriverMock();
$entityManager = $this->_createEntityManager($mockDriver);
$cmf = new ClassMetadataFactoryTestSubject();
$cmf->setEntityManager($entityManager);
return $cmf;
}
}
/* Test subject class with overriden factory method for mocking purposes */
@ -178,3 +219,7 @@ class TestEntity1
private $other;
private $association;
}
class CustomIdGenerator extends \Doctrine\ORM\Id\AbstractIdGenerator {
public function generate(\Doctrine\ORM\EntityManager $em, $entity) {}
}