fasano / phprimitives-doctrine
Doctrine extension for PHPrimitives.
1.0.0
2026-03-11 19:06 UTC
Requires
- doctrine/dbal: ^4.4
- fasano/phprimitives: ^1.0
README
A set of Doctrine DBAL types to map any PHPrimitive with almost no boilerplate.
Installation
composer require fasano/phprimitives-doctrine
Creating a Custom Type
Extend the abstract type that matches your primitive's scalar type, then implement two methods:
use Fasano\PHPrimitives\Doctrine\StringPrimitiveType; final class EmailType extends StringPrimitiveType { public function getName(): string { return 'email'; } protected function getPrimitiveClass(): string { return Email::class; } }
Registering Types
Register your custom types, as usual:
use Doctrine\DBAL\Types\Type; Type::addType('email', EmailType::class); Type::addType('age', AgeType::class); Type::addType('status', StatusType::class);
Symfony — register via config/packages/doctrine.yaml instead:
doctrine: dbal: types: email: Infra\Doctrine\Type\EmailType age: Infra\Doctrine\Type\AgeType status: Infra\Doctrine\Type\StatusType
Using The Types
Reference the type name in your #[Column] attribute:
use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] class User { #[ORM\Column(type: 'email')] public Email $email; #[ORM\Column(type: 'age')] public Age $age; #[ORM\Column(type: 'status')] public Status $status; }
Doctrine will call construct() when hydrating from the database and deconstruct() when persisting, so your domain invariants are enforced on every read and write.