From 0a835609fabd9ef600127c4cacfbcc776d31d981 Mon Sep 17 00:00:00 2001 From: Maarten de Keizer Date: Mon, 19 Sep 2011 18:29:24 +0200 Subject: [PATCH 1/4] UUID id generator --- lib/Doctrine/ORM/Id/UuidGenerator.php | 49 +++++++++++++++++++ .../ORM/Mapping/ClassMetadataFactory.php | 3 ++ .../ORM/Mapping/ClassMetadataInfo.php | 15 ++++++ 3 files changed, 67 insertions(+) create mode 100644 lib/Doctrine/ORM/Id/UuidGenerator.php diff --git a/lib/Doctrine/ORM/Id/UuidGenerator.php b/lib/Doctrine/ORM/Id/UuidGenerator.php new file mode 100644 index 000000000..4b1d6ded3 --- /dev/null +++ b/lib/Doctrine/ORM/Id/UuidGenerator.php @@ -0,0 +1,49 @@ +. + */ + +namespace Doctrine\ORM\Id; + +use Serializable, Doctrine\ORM\EntityManager; + +/** + * Represents an ID generator that uses a database sequence. + * + * @since 2.0 + * @author Roman Borschel + * @author Maarten de Keizer + */ +class UuidGenerator extends AbstractIdGenerator +{ + + /** + * Generates an ID for the given entity. + * + * @param Doctrine\ORM\EntityManager $em The EntityManager to user + * @param object $entity + * @return string The generated value. + * @override + */ + public function generate(EntityManager $em, $entity) + { + $conn = $em->getConnection(); + $sql = 'SELECT ' . $conn->getDatabasePlatform()->getGuidExpression(); + return $conn->query($sql)->fetchColumn(0); + } + +} diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php index 815808014..54ce6ff34 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php @@ -515,6 +515,9 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface case ClassMetadata::GENERATOR_TYPE_NONE: $class->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator()); break; + case ClassMetadata::GENERATOR_TYPE_UUID: + $class->setIdGenerator(new \Doctrine\ORM\Id\UuidGenerator()); + break; case ClassMetadata::GENERATOR_TYPE_TABLE: throw new ORMException("TableGenerator not yet implemented."); break; diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index 40614c7d2..a52bdb8a1 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -94,6 +94,11 @@ class ClassMetadataInfo implements ClassMetadata * must have a natural, manually assigned id. */ const GENERATOR_TYPE_NONE = 5; + /** + * UUID means that a UUID/GUID expression is used for id generation. Full + * portability is currently not guaranteed. + */ + const GENERATOR_TYPE_UUID = 6; /** * DEFERRED_IMPLICIT means that changes of entities are calculated at commit-time * by doing a property-by-property comparison with the original data. This will @@ -1559,6 +1564,16 @@ class ClassMetadataInfo implements ClassMetadata { return $this->generatorType == self::GENERATOR_TYPE_NONE; } + + /** + * Checks whether the class use a UUID for id generation + * + * @return boolean + */ + public function isIdentifierUuid() + { + return $this->generatorType == self::GENERATOR_TYPE_UUID; + } /** * Gets the type of a field. From 33c5f4f678798853fa81fbad6624ed6987c2c68e Mon Sep 17 00:00:00 2001 From: Maarten de Keizer Date: Mon, 12 Mar 2012 11:11:06 +0100 Subject: [PATCH 2/4] Fix comments --- lib/Doctrine/ORM/Id/UuidGenerator.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Id/UuidGenerator.php b/lib/Doctrine/ORM/Id/UuidGenerator.php index 4b1d6ded3..b4ae37773 100644 --- a/lib/Doctrine/ORM/Id/UuidGenerator.php +++ b/lib/Doctrine/ORM/Id/UuidGenerator.php @@ -22,10 +22,9 @@ namespace Doctrine\ORM\Id; use Serializable, Doctrine\ORM\EntityManager; /** - * Represents an ID generator that uses a database sequence. + * Represents an ID generator that uses the database UUID expression * - * @since 2.0 - * @author Roman Borschel + * @since 2.3 * @author Maarten de Keizer */ class UuidGenerator extends AbstractIdGenerator From d57159ad549d3b54b0c13c56b12949482b98c5c9 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Mon, 12 Mar 2012 12:48:14 +0100 Subject: [PATCH 3/4] [DDC-451] Add test for UUIDGenerator --- .../ORM/Functional/UUIDGeneratorTest.php | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/Doctrine/Tests/ORM/Functional/UUIDGeneratorTest.php diff --git a/tests/Doctrine/Tests/ORM/Functional/UUIDGeneratorTest.php b/tests/Doctrine/Tests/ORM/Functional/UUIDGeneratorTest.php new file mode 100644 index 000000000..5fa237ee4 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/UUIDGeneratorTest.php @@ -0,0 +1,48 @@ +_em->getConnection()->getDatabasePlatform()->getName() != 'mysql') { + $this->markTestSkipped('Currently restricted to MySQL platform.'); + } + + $this->_schemaTool->createSchema(array( + $this->_em->getClassMetadata(__NAMESPACE__ . '\\UUIDEntity') + )); + } + + public function testGenerateUUID() + { + $entity = new UUIDEntity(); + + $this->_em->persist($entity); + $this->assertNotNull($entity->getId()); + $this->assertTrue(strlen($entity->getId()) > 0); + } +} + +/** + * @Entity + */ +class UUIDEntity +{ + /** @Id @Column(type="string") @GeneratedValue(strategy="UUID") */ + private $id; + /** + * Get id. + * + * @return id. + */ + public function getId() + { + return $this->id; + } +} From 23d004844ae8065bf8e80675467a0e1ed9a2e116 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Mon, 12 Mar 2012 12:52:36 +0100 Subject: [PATCH 4/4] [DDC-451] Adjust doctrine-mapping.xsd for new UUID generator --- doctrine-mapping.xsd | 1 + 1 file changed, 1 insertion(+) diff --git a/doctrine-mapping.xsd b/doctrine-mapping.xsd index 96f155a60..937d5a4ec 100644 --- a/doctrine-mapping.xsd +++ b/doctrine-mapping.xsd @@ -161,6 +161,7 @@ +