guikingone / symfony-uid-doctrine
Allow the use of symfony/uid as Doctrine field type
Installs: 1 874
Dependents: 0
Suggesters: 0
Security: 0
Stars: 12
Watchers: 2
Forks: 0
Open Issues: 1
Requires
- php: >=7.2
- doctrine/orm: ^2.5
- symfony/uid: ^5.1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- infection/infection: ^0.15.3
- phpstan/phpstan: ^0.12.30
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2024-12-12 01:01:44 UTC
README
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.