ramsey/uuid-doctrine

Allow the use of a ramsey/uuid UUID as Doctrine field type.

1.4.1 2017-07-18 16:21 UTC

README

Source Code Latest Version Software License Build Status Coverage Status Total Downloads

The ramsey/uuid-doctrine package provides the ability to use ramsey/uuid as a Doctrine field type.

This project adheres to a Contributor Code of Conduct. By participating in this project and its community, you are expected to uphold this code.

Installation

The preferred method of installation is via Packagist and Composer. Run the following command to install the package and add it as a requirement to your project's composer.json:

composer require ramsey/uuid-doctrine

Examples

Configuration

To configure Doctrine to use ramsey/uuid as a field type, you'll need to set up the following in your bootstrap:

\Doctrine\DBAL\Types\Type::addType('uuid', 'Ramsey\Uuid\Doctrine\UuidType');

In Symfony:

# app/config/config.yml
doctrine:
   dbal:
       types:
           uuid:  Ramsey\Uuid\Doctrine\UuidType

In Zend Framework:

<?php 
// module.config.php
use Ramsey\Uuid\Doctrine\UuidType;

return [
    'doctrine' => [
        'configuration' => [
            'orm_default' => [
                'types' => [
                    UuidType::NAME => UuidType::class,

Usage

Then, in your models, you may annotate properties by setting the @Column type to uuid, and defining a custom generator of Ramsey\Uuid\UuidGenerator. Doctrine will handle the rest.

/**
 * @Entity
 * @Table(name="products")
 */
class Product
{
    /**
     * @var \Ramsey\Uuid\Uuid
     *
     * @Id
     * @Column(type="uuid", unique=true)
     * @GeneratedValue(strategy="CUSTOM")
     * @CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
     */
    protected $id;

    public function getId()
    {
        return $this->id;
    }
}

If you use the XML Mapping instead of PHP annotations.

<id name="id" column="id" type="uuid">
    <generator strategy="CUSTOM"/>
    <custom-id-generator class="Ramsey\Uuid\Doctrine\UuidGenerator"/>
</id>

You can also use the YAML Mapping.

id:
    id:
        type: uuid
        generator:
            strategy: CUSTOM
        customIdGenerator:
            class: Ramsey\Uuid\Doctrine\UuidGenerator

Binary Database Columns

In the previous example, Doctrine will create a database column of type CHAR(36), but you may also use this library to store UUIDs as binary strings. The UuidBinaryType helps accomplish this.

In your bootstrap, place the following:

\Doctrine\DBAL\Types\Type::addType('uuid_binary', 'Ramsey\Uuid\Doctrine\UuidBinaryType');
$entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('uuid_binary', 'binary');

In Symfony:

# app/config/config.yml
doctrine:
   dbal:
       types:
           uuid_binary:  Ramsey\Uuid\Doctrine\UuidBinaryType
       mapping_types:
           uuid_binary: binary

Then, when annotating model class properties, use uuid_binary instead of uuid:

@Column(type="uuid_binary")

InnoDB-optimised binary UUIDs

More suitable if you want to use UUIDs as primary key. Note that this can cause unintended effects if

  • decoding bytes that were not generated using this method
  • another code (that isn't aware of this method) attempts to decode the resulting bytes

More information in this Percona article and UUID Talk by Ben Ramsey (starts at slide 61).

\Doctrine\DBAL\Types\Type::addType('uuid_binary_ordered_time', 'Ramsey\Uuid\Doctrine\UuidBinaryType');
$entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('uuid_binary_ordered_time', 'binary');

In Symfony:

# app/config/config.yml
doctrine:
   dbal:
       types:
           uuid_binary_ordered_time: Ramsey\Uuid\Doctrine\UuidBinaryOrderedTimeType
       mapping_types:
           uuid_binary_ordered_time: binary

Then, in your models, you may annotate properties by setting the @Column type to uuid_binary_ordered_time, and defining a custom generator of Ramsey\Uuid\UuidOrderedTimeGenerator. Doctrine will handle the rest.

/**
 * @Entity
 * @Table(name="products")
 */
class Product
{
    /**
     * @var \Ramsey\Uuid\Uuid
     *
     * @Id
     * @Column(type="uuid_binary_ordered_time", unique=true)
     * @GeneratedValue(strategy="CUSTOM")
     * @CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidOrderedTimeGenerator")
     */
    protected $id;

    public function getId()
    {
        return $this->id;
    }
}

If you use the XML Mapping instead of PHP annotations.

<id name="id" column="id" type="uuid_binary_ordered_time">
    <generator strategy="CUSTOM"/>
    <custom-id-generator class="Ramsey\Uuid\Doctrine\UuidOrderedTimeGenerator"/>
</id>

More Information

For more information on getting started with Doctrine, check out the "Getting Started with Doctrine" tutorial.

Contributing

Contributions are welcome! Please read CONTRIBUTING for details.

Copyright and License

The ramsey/uuid-doctrine library is copyright © Ben Ramsey and licensed for use under the MIT License (MIT). Please see LICENSE for more information.