Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

DDC-451 - Add GUID/UUID Id Generator #127

Closed
wants to merge 2 commits into from

6 participants

@maartendekeizer

No description provided.

@Jehu

+1

@Ocramius Ocramius commented on the diff
lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
@@ -94,6 +94,11 @@ class ClassMetadataInfo implements ClassMetadata
*/
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;
+ /**
@Ocramius Owner
Ocramius added a note

Missing whitespace here

??? i don't see any missing whitespace...

@Ocramius Owner

Maybe it is me misunderstanding the CS... All these constants collapsed that way :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/Id/UuidGenerator.php
((12 lines not shown))
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the LGPL. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+
+namespace Doctrine\ORM\Id;
+
+use Serializable, Doctrine\ORM\EntityManager;
+
+/**
+ * Represents an ID generator that uses a database sequence.
+ *
+ * @since 2.0
@Ocramius Owner
Ocramius added a note

Maybe this will be 2.3...

Maybe; thats up to the Doctrine team. Need to change that later when the PR is accepted,

@stof
stof added a note

Well, it canot be added since 2.0 as it is not even part of 2.2 which has already been released. Either it is merged and it will be part of 2.3 either it is not merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius
Owner

This is really nice, and simple too! :D
I like it! +1!

I'm just throwing the idea for (eventually) a new PR at you: could you make the generator instantiation logic a bit more flexible? I'm especially talking about https://github.com/maartendekeizer/doctrine2/blob/84a35f806f3935e76ee987ffa234ec1aacd953fa/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php#L448 ... Maybe that shouldn't always be an int...

@maartendekeizer

The only way to add a new generator at this moment is hard coded declaration, that is very fast but hard to manage.
I think a complete new more dynamic way (like custom data types) is better and make it easier to manage new and custom generators. But implementing a dynamic system for ID generation is a large operation and need some discussion first.

@asm89
Collaborator

Nice PR. I want to merge it, but before I do it needs some tests. Could you perhaps add some tests to your PR? (And change the @since to 2.3. ;) )

@beberlei beberlei closed this
@beberlei
Owner

This was merged into master, i rebased it before, added a test and a phpunit.xml, so the PR hasn't closed automatically.

@maartendekeizer

TY for adding test and merging into master!

@elHornair elHornair referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@elHornair elHornair referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@gerryvdm gerryvdm referenced this pull request from a commit in gerryvdm/orm-documentation
@gerryvdm gerryvdm Add documentation for UUID ID generation
While UUID generation has been a valid strategy for some time now (doctrine/doctrine2#127), it seemed to be still missing from the docs here :)
8802380
@gerryvdm gerryvdm referenced this pull request in doctrine/orm-documentation
Closed

Add documentation for UUID ID generation #165

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 19, 2011
  1. @maartendekeizer

    UUID id generator

    maartendekeizer authored
Commits on Mar 12, 2012
  1. @maartendekeizer

    Fix comments

    maartendekeizer authored Maarten de Keizer committed
This page is out of date. Refresh to see the latest.
View
48 lib/Doctrine/ORM/Id/UuidGenerator.php
@@ -0,0 +1,48 @@
+<?php
+/*
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the LGPL. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+
+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 <m.de.keizer@markei.nl>
+ */
+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);
+ }
+
+}
View
3  lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
@@ -474,6 +474,9 @@ private function completeIdGeneratorMapping(ClassMetadataInfo $class)
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;
View
15 lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
@@ -94,6 +94,11 @@ class ClassMetadataInfo implements ClassMetadata
*/
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;
+ /**
@Ocramius Owner
Ocramius added a note

Missing whitespace here

??? i don't see any missing whitespace...

@Ocramius Owner

Maybe it is me misunderstanding the CS... All these constants collapsed that way :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
* 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
* be done for all entities that are in MANAGED state at commit-time.
@@ -1229,6 +1234,16 @@ public function isIdentifierNatural()
{
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.
Something went wrong with that request. Please try again.