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., (*1)
The gubler/guid-doctrine package provides the ability to use
ramsey/uuid as a Doctrine field type., (*2)
This project adheres to a Contributor Code of Conduct. By participating in this project and its community, you are expected to uphold this code., (*3)
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
:, (*4)
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:, (*5)
``` php
\Doctrine\DBAL\Types\Type::addType('uuid', 'Gubler\Guid\Doctrine\GuidType');, (*6)
In Symfony:
``` yaml
# config/packages/doctrine.yaml
doctrine:
dbal:
types:
guid: Gubler\Guid\Doctrine\GuidType
In Zend Framework:, (*7)
<?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., (*8)
``` php
/**
* @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;, (*9)
public function getId()
{
return $this->id;
}
}, (*10)
If you use the XML Mapping instead of PHP annotations.
``` XML
<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.
``` yaml
id:
id:
type: guid
generator:
strategy: CUSTOM
customIdGenerator:
class: Gubler\Guid\Doctrine\GuidGenerator, (*11)
### 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:
``` php
\Doctrine\DBAL\Types\Type::addType('guid_binary', 'Gubler\Guid\Doctrine\GuidBinaryType');
$entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('guid_binary', 'binary');
In Symfony:
``` yaml, (*12)
config/packages/doctrine.yaml
doctrine:
dbal:
types:
guid_binary: Gubler\Guid\Doctrine\GuidBinaryType
mapping_types:
guid_binary: binary
```, (*13)
Then, when annotating model class properties, use guid_binary
instead of guid
:, (*14)
@Column(type="guid_binary")
For more information on getting started with Doctrine, check out the "Getting
Started with Doctrine" tutorial., (*15)
Contributing
Contributions are welcome! Please read CONTRIBUTING for details., (*16)
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., (*17)