Skip to content

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

License

Notifications You must be signed in to change notification settings

fluoresceco/uuid-doctrine

 
 

Repository files navigation

ramsey/uuid-doctrine

Gitter Chat Source Code Latest Version Software License Build Status HHVM Status Scrutinizer 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

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');
$entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('uuid', 'uuid');

Then, in your models, you may annotate properties by setting the @Column type to uuid. Depending on your database engine, you may not be able to auto-generate a UUID when inserting into the database, but this isn't a problem; in your model's constructor (or elsewhere, depending on how you create instances of your model), generate a Ramsey\Uuid\Uuid object for the property. Doctrine will handle the rest.

For example, here we annotate an @Id column with the uuid type, and in the constructor, we generate a version 4 UUID to store for this entity.

/**
 * @Entity
 * @Table(name="products")
 */
class Product
{
    /**
     * @var \Ramsey\Uuid\Uuid
     *
     * @Id
     * @Column(type="uuid")
     * @GeneratedValue(strategy="NONE")
     */
    protected $id;

    public function __construct()
    {
        $this->id = \Ramsey\Uuid\Uuid::uuid4();
    }

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

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');

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

@Column(type="uuid_binary")

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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%