Skip to content

gubler/guid-doctrine

Repository files navigation

gubler/guid-doctrine

This is based off of the ramsey/uuid-doctrine project. The only thing this project does differently is return GUIDs. This to handle Active Directory GUIDs.


The gubler/guid-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 gubler/guid-doctrine

Examples

Configuration

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

\Doctrine\DBAL\Types\Type::addType('uuid', 'Gubler\Guid\Doctrine\GuidType');

In Symfony:

# config/packages/doctrine.yaml
doctrine:
   dbal:
       types:
           guid:  Gubler\Guid\Doctrine\GuidType

In Zend Framework:

<?php 
// module.config.php
use Gubler\Guid\Doctrine\GuidType;

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

Usage

Then, in your models, you may annotate properties by setting the @Column type to guid, and defining a custom generator of Gubler\Guid\GuidGenerator. Doctrine will handle the rest.

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

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

If you use the XML Mapping instead of PHP annotations.

<id name="id" column="id" type="guid">
    <generator strategy="CUSTOM"/>
    <custom-id-generator class="Gubler\Guid\Doctrine\GuidGenerator"/>
</id>

You can also use the YAML Mapping.

id:
    id:
        type: guid
        generator:
            strategy: CUSTOM
        customIdGenerator:
            class: Gubler\Guid\Doctrine\GuidGenerator

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 GUIDs as binary strings. The GuidBinaryType helps accomplish this.

In your bootstrap, place the following:

\Doctrine\DBAL\Types\Type::addType('guid_binary', 'Gubler\Guid\Doctrine\GuidBinaryType');
$entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('guid_binary', 'binary');

In Symfony:

# config/packages/doctrine.yaml
doctrine:
   dbal:
       types:
           guid_binary:  Gubler\Guid\Doctrine\GuidBinaryType
       mapping_types:
           guid_binary: binary

Then, when annotating model class properties, use guid_binary instead of guid:

@Column(type="guid_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 gubler/guid-doctrine library is copyright © Daryl Gubler and licensed for use under the MIT License (MIT). Please see LICENSE for more information.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages