Skip to content

A small and fast library which aim to provide Symfony/Uid support in Doctrine as a custom type

Notifications You must be signed in to change notification settings

Guikingone/SymfonyUidDoctrine

Repository files navigation

guikingone/symfony-uid-doctrine

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.

About

A small and fast library which aim to provide Symfony/Uid support in Doctrine as a custom type

Resources

Stars

Watchers

Forks

Packages

No packages published