pchapl / ids
Symfony bundle for doctrine custom types autoconfiguration
Installs: 28
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^8.0.0
- doctrine/dbal: ^3.0|^2.8.0
- symfony/config: ^5.0|^6.0
- symfony/dependency-injection: ^5.0|^6.0
- symfony/http-kernel: ^5.0|^6.0
Requires (Dev)
- phpunit/phpunit: ^9.5
- roave/security-advisories: dev-latest
README
Usage
composer require pchapl/ids
Enable bundle in config/bundles.php
:
PChapl\DoctrineIdBundle\DoctrineIdBundle::class => ['all' => true],
Create Id classes like
namespace App\Data; use PChapl\DoctrineIdBundle\Id\Base; final class AccountId extends Base { }
Configure the bundle with types in the config/packages/doctrine_id.yaml
:
doctrine_id: types: account_id: App\Data\AccountId user_id: App\Data\UserId
Then just use in entities
#[ORM\Entity] class Account { #[ORM\Column(type="account_id")] private AccountId $id; public function __construct(AccountId $id) { $this->id = $id; } public function getId(): AccountId { return $this->id; }
There are two ways to instantiate ids: implement Factory interface for Base::new
method or just call Base::fromValue
with generated string id:
/** @var \Hidehalo\Nanoid\Client $nanoidClient */ $factory = new class($nanoidClient) implements \PChapl\DoctrineIdBundle\Id\Factory { public function __construct(private \Hidehalo\Nanoid\Client $nano) { } public function generate(): string { return $this->nano->generateId(); } }; $account1 = new Account(AccountId::new($factory)); $account2 = new Account(AccountId::fromValue($nanoidClient->generateId())); $account3 = new Account(AccountId::fromValue(\Ramsey\Uuid\Uuid::uuid4()->toString()));