Skip to content

Loading…

DDC-451: Add GUID/UUID Id Generator #4953

Closed
doctrinebot opened this Issue · 19 comments

2 participants

@doctrinebot

Jira issue originally created by user @beberlei:

We should add an IdGenerator that facilitates the DB Vendors UUID()/GUID() generation facilities.

Although these IdGenerators take more space compared to other Generators there are use-cases which can be only solved with this kind of Id Generator.

@doctrinebot

Comment created by @guilhermeblanco:

Here is a basic GUID generator.

class Guid
{
    /****
     * Generate a GUID
     *
     * @return string
     */
    public static function generate()
    {
        $cpuname     = getenv('COMPUTERNAME');
        $address     = isset($*SERVER['SERVER_ADDR']) ? @$_SERVER['SERVER*ADDR'] : uniqid(hash("md5", time()), true) . time();
        $address     = (trim($cpuname) == '' ? crypt(uniqid(rand(), true)) : $cpuname) . '/' . $address;
        $milisecs        = microtime();
        $randomLong  = (rand(0, 1))? '-':'';
        $randomLong .= rand(1000, 9999).rand(1000, 9999).rand(1000, 9999).rand(100, 999).rand(100, 999);

        $string = $address . ':' . $milisecs . ':' . $randomLong;
        $hashString = strtoupper(md5($string));

        return substr($hashString, 0, 8).'-'.substr($hashString, 8, 4).'-'.substr($hashString, 12, 4).'-'.substr($hashString, 16, 4).'-'.substr($hashString, 20);
    }


    /****
     * Verifies if a string is a valid Guid generated acording to this class
     *
     * @param string $guid Guid to be analyzed
     * @return boolean
     */
    public static function match($guid)
    {

        $match = preg_match("/^([A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12})$/", $guid);

        return $match;
    }
}
@doctrinebot

Comment created by andyajadeh:

I agree, there are some application features that can only be done with GUID.

@doctrinebot

Comment created by steffenvogel:

Oh, thats a quite simple implementation of a UUID/GUID generator.

I would prefer something like this:
http://github.com/volkszaehler/volkszaehler.org/blob/master/backend/lib/Util/UUID.php

Its a RFC2144 compatible implementation for UUIDs

@doctrinebot

Comment created by maartendekeizer:

A couple of months ago I created 2 pull requests for this feature (#127 and doctrine/dbal#58). It would be nice if the feature is in the next Doctrine release.

@doctrinebot

Comment created by @beberlei:

Implemented

@doctrinebot

Issue was closed with resolution "Fixed"

@doctrinebot

Comment created by @beberlei:

Details: This works with Database provided GUID functionality only for now, the UUID Generator uses the AbstractPlatform#getGuidSqlExpression() function and a SELECT clause to fetch a new guid. A next step might be to add a Platform#supportsGuidGeneration() flag and otherwise fallback to a PHP based generation.

The Id generator in the ORM is pre insert, which makes this very awesome :-)

@doctrinebot

Comment created by marijn:

There is a nice implementation out there with proper UUID generation and a DBAL datatype.

@doctrinebot

Comment created by @beberlei:

starting with 2.3 there are custom id generators and you can do whatever you want. There is also a UUID/GUID type in 2.3 that is using the appropriate vendor database functions to generate those values.

@doctrinebot

Comment created by holtkamp:

{quote}
There is also a UUID/GUID type in 2.3 that is using the appropriate vendor database functions to generate those values.
{quote}

So how would one be able to use this UUID/GUID? Would it be required for the column to be tagged as a @Id and @GeneratedValue with GUID strategy? I would like to have a random string generated which does not act as @Id within Doctrine... The text quoted suggests that it should work out of the box by setting the column type to 'guid', which it does not (2.3.0-RC1).

Simply setting the column type to 'guid' does not trigger the database to generate an ID when INSERTing a row...

@doctrinebot

Comment created by @ocramius:

I'm not sure about PHP's case sensitivity, but as of https://github.com/doctrine/doctrine2/blob/00a5f185444fb5d7c84e445d9ea6d6a68e087f98/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php#L310 I think you should try with "UUID" (uppercase)

@doctrinebot

Comment created by holtkamp:

Marco, it is indeed 'UUID', but this seems not to kick in when there is no @Id annotation used. I want to use it to come up with an identifier for an Order. It is not a primary key at database level or something like that. I was just wondering whether this new functionality can be used like that. I get the feeling it is not intended to be used like this:

/****
 * Unique Order identifier that acts as reference for an Order even when multiple databases are involved
 * 
 * @var string
 * @ORM\Column(name="order_identifier", type="string", unique=true)
 * @ORM\GeneratedValue(strategy="UUID")
 */
protected $_orderIdentifier;
@doctrinebot

Comment created by @beberlei:

Well it is an ID generator. So it has to be with @Id.

@doctrinebot

Comment created by holtkamp:

Well, yes, that was my question and that is now answered, thanks.

It would be nice though to have UUID generation functionality available for non-database specific IDs as well. For instance for:

  • user account activation tokens
  • payment references
  • invoice reference, etc.

This is now something that has to be implemented at the applciation level. But since we have these generators available, why not re-use it. By adding a @Id annotation, it becomes part of a composite key, which is not the purpose. Maybe I'll dive into the code and see whether this is possible.

@doctrinebot

Comment created by @beberlei:

it is available, just take a look at the code.

@doctrinebot

Comment created by holtkamp:

{quote}Well it is an ID generator. So it has to be with @Id.{quote}
{quote}it is available, just take a look at the code.{quote}
Mmm, now I am confused, I think we are mis-communicating.

Maybe using less text and more descriptive examples:

  • is it possible to use Database GUID functionality **** without using the @Id annotations, to generate values for Entity properties that are not a primary key? **** note that this Entity unique reference would exist next to a property that does have the @Id annotation

Attempt 1:

/****
 * The Order ID used at database level, which does NOT have any meaning within our domain, solely at database level
 *
 * @var integer
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 * @ORM\SequenceGenerator(sequenceName="orders*id*seq")
 */
protected $_id;

/****
 * The Order reference used at domain level, which DOES have meaning in our domain
 * Typically used to report in emails, printed on Invoices, etc
 *
 * @var string
 * @ORM\Column(name="order_identifier", type="guid")
 */
protected $_orderIdentifier;

Attempt 2:

/****
 * The Order ID used at database level, which does NOT have meaning within our domain, solely at database level
 *
 * @var integer
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 * @ORM\SequenceGenerator(sequenceName="orders*id*seq")
 */
protected $_id;

/****
 * The Order reference used at domain level, which DOES have meaning in our domain
 * Typically used to report in emails, printed on Invoices, etc
 *
 * @var string
 * @ORM\Column(name="order_identifier", type="string", unique=true)
 * @ORM\GeneratedValue(strategy="UUID")
 */
protected $_orderIdentifier;

Based on Benjamin's first answer I guess that this is not possible. Point is, the second answer indicates that it is possible ;). What is it?

Also note that in Attempt 2, the @GeneratedValue annotation on the second property interferes with the ID generation of the Entity defined at the first property. This is probably to realize composite keys, but can result in unexpected behavior since it replaces the way the primary key is generated?

@doctrinebot

Comment created by @beberlei:

Well @GeneratedValue Only works with @Id as I said. But there are low level APIs that you can look up in the UuidGenerator Strategy to use for something like this.

@beberlei beberlei was assigned by doctrinebot
@doctrinebot doctrinebot added this to the 2.3 milestone
@doctrinebot doctrinebot closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.