Symfony bundle for generating and validating ID types
This version of the project requires:
- PHP 8.3+
- Symfony 6.4+
You can install the library through composer:
composer require skrepr/id-type
The bundle should be enabled by symfony/flex, but if not:
// config/bundles.php
<?php
return [
Skrepr\IdType\SkreprIdTypeBundle::class => ['all' => true],
];
To generate an UuidType:
bin/console make:id-type [--register] <id_name>
Where id_name
is something like "user_id".
With this maker command, two files are generated (src/ValueObject/UserId.php
and src/Persistence/Doctrine/UserIdType.php
)
and if the --register
option is given, also the new type is added to config/packages/doctrine.yaml
.
Registering is not needed if you are using autoconfigure because of the service tag "skrepr.id-type" will automatically register the type to doctrine.
To use this new id in your entity (example:
<?php
declare(strict_types=1);
namespace App\Entity;
use App\ValueObject\UserId;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class User
{
#[ORM\Id]
#[ORM\Column(type: UserId::TYPE)]
public readonly UserId $id;
#[ORM\Column(type: 'string')]
public string $name;
public function __construct(string $name)
{
$this->id = UserId::generate();
$this->name = $name;
}
}
See the example directory for the generated files by make:id-type.
To generate a new ID you can use the static generate
-function:
$newId = UserId::generate();
You can also feed a UUID or string to the constructor:
$userId = new UserId('00000000-0000-0000-0000-000000000000');
// or
$userId = new UserId( \Symfony\Component\Uid\Uuid::v4() );
By default, the created ID's can be used with AutoConfigure from Symfony. To upgrade existing ID's to this system you have to do the following (in this example we use "UserId").
- Remove the 'user_id'-line from config/packages/doctrine.yaml
doctrine: dbal: types: user_id: App\Persistence\Doctrine\UserIdType
- Add the service tag 'skrepr.id-type' to App\Persistence\Doctrine\UserIdType
#[AutoconfigureTag('skrepr.id-type')] class UserIdType extends AbstractUuidType
- (optional) Remove the function App\Persistence\Doctrine\UserIdType::getName
- (optional) Add a constant to App\ValueObject\TestId (required if step 3 is done)
public const string TYPE = 'user_id';
Instead of step 2, you can also add the tag to all your custom types at once, because the compiler pass of skrepr/id-type will check for a subclass of AbstractUuidType:
# config/service.yaml
App\Persistence\Doctrine\:
resource: '../src/Persistence/Doctrine/'
tags:
- { name: 'skrepr.id-type' }