From 0a835609fabd9ef600127c4cacfbcc776d31d981 Mon Sep 17 00:00:00 2001 From: Maarten de Keizer Date: Mon, 19 Sep 2011 18:29:24 +0200 Subject: [PATCH] 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.