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 @@ + diff --git a/lib/Doctrine/ORM/Id/UuidGenerator.php b/lib/Doctrine/ORM/Id/UuidGenerator.php new file mode 100644 index 000000000..b4ae37773 --- /dev/null +++ b/lib/Doctrine/ORM/Id/UuidGenerator.php @@ -0,0 +1,48 @@ +. + */ + +namespace Doctrine\ORM\Id; + +use Serializable, Doctrine\ORM\EntityManager; + +/** + * Represents an ID generator that uses the database UUID expression + * + * @since 2.3 + * @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. 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; + } +}