guikingone/symfony-uid-doctrine

Allow the use of symfony/uid as Doctrine field type

0.1.1 2020-07-11 15:24 UTC

This package is auto-updated.

Last update: 2024-04-11 23:36:29 UTC


README

SymfonyUidDoctrine SymfonyUidDoctrine - Configuration

The guikingone/symfony-uid-doctrine aim to provide the support of Symfony/Uid as a Doctrine type.

Installation

Consider using Packagist and Composer when installing the following project:

composer require guikingone/symfony-uid-doctrine

Configuration

Uuid

Doctrine\DBAL\Types\Type::addType('uuid', Guikingone\SymfonyUid\Doctrine\Uuid\UuidType::class);
# config/packages/doctrine.yaml
doctrine:
    dbal:
        types:
            uuid: Guikingone\SymfonyUid\Doctrine\Uuid\UuidType

Ulid

Doctrine\DBAL\Types\Type::addType('ulid', Guikingone\SymfonyUid\Doctrine\Ulid\UlidType::class);
# config/packages/doctrine.yaml
doctrine:
    dbal:
        types:
            ulid: Guikingone\SymfonyUid\Doctrine\Ulid\UlidType

Usage

Uuid

use Doctrine\ORM\Mapping as ORM;
use Guikingone\SymfonyUid\Doctrine\Uuid\UuidGenerator;

/**
 * @ORM\Entity
 * @ORM\Table(name="posts")
 */
class Post
{
    /**
     * @var Symfony\Component\Uid\Uuid
     *
     * @ORM\Id
     * @ORM\Column(type="uuid", unique=true)
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class=UuidGenerator::class)
     */
    protected $id;

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

If you don't want to use the generator, consider using the __construct() method:

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

/**
 * @ORM\Entity
 * @ORM\Table(name="posts")
 */
class Post
{
    /**
     * @var Symfony\Component\Uid\Uuid
     *
     * @ORM\Id
     * @ORM\Column(type="uuid", unique=true)
     */
    protected $id;

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

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

Ulid

use Doctrine\ORM\Mapping as ORM;
use Guikingone\SymfonyUid\Doctrine\Ulid\UlidGenerator;

/**
 * @ORM\Entity
 * @ORM\Table(name="posts")
 */
class Post
{
    /**
     * @var Symfony\Component\Uid\Ulid
     *
     * @ORM\Id
     * @ORM\Column(type="ulid", unique=true)
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class=UlidGenerator::class)
     */
    protected $id;

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

If you don't want to use the generator, consider using the __construct() method:

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Ulid;

/**
 * @ORM\Entity
 * @ORM\Table(name="posts")
 */
class Post
{
    /**
     * @var Symfony\Component\Uid\Ulid
     *
     * @ORM\Id
     * @ORM\Column(type="ulid", unique=true)
     */
    protected $id;

    public function __construct() 
    {
        $this->id = new Ulid();
    }

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

Contributing

Contributions are welcome! Please read CONTRIBUTING for details.