k2gl / entity-exist
Checks if there is at least one row with the required field in the database, for example, a row with the same identifier
Requires
- php: ^8.1
- doctrine/orm: ^2.13|^3.0
- symfony/dependency-injection: ^6.1|^7.0|^8.0
- symfony/validator: ^6.1|^7.0|^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.64
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.5|^11.0|^12.0
- rector/rector: ^2.0
- roave/security-advisories: dev-latest
- symfony/phpunit-bridge: ^6.1|^7.0|^8.0
- symfony/var-dumper: ^6.1|^7.0|^8.0
README
Requirements
- PHP 8.1+
- Symfony 6.1, 7.x or 8.x (
symfony/validator,symfony/dependency-injection) - Doctrine ORM 2.13+ or 3.x
Installation
You can add this library as a local, per-project dependency to your project using Composer:
composer require k2gl/entity-exist
Configuration
Makes classes in src/ available to be used as services in services.yaml
services:
K2gl\Component\Validator\Constraint\EntityExist\:
resource: '../vendor/k2gl/entity-exist/src/'
arguments: ['@doctrine.orm.entity_manager']
tags:
- { name: validator.constraint_validator }
Usage
AssertEntityNotExist
use K2gl\Component\Validator\Constraint\EntityExist\AssertEntityNotExist; readonly class RegisterUserOrder { public function __construct( #[Assert\NotBlank] #[Assert\Email] #[AssertEntityNotExist( entity: User::class, property: 'email', message: 'User with email "%value%" already registered.' )] public string $email, ) { } }
AssertEntityExist
use K2gl\Component\Validator\Constraint\EntityExist\AssertEntityExist; readonly class TransferUserToOtherUnitOrder { public function __construct( #[Assert\NotBlank] #[AssertEntityExist( entity: User::class, property: 'uuid', )] public string $user, #[Assert\NotBlank] #[AssertEntityExist( entity: Unit::class, property: 'uuid', )] public string $unit, ) { } }
Violation codes
Each constraint declares the violation code it emits as a UUID constant on its
own class (AssertEntityExist::NOT_EXIST, AssertEntityNotExist::EXIST).
Reading those at the call site can be awkward — especially
AssertEntityNotExist::EXIST, where the class name and the constant negate
each other.
For nicer reading in error handling and tests, the same codes are also exposed
under neutral names on ViolationCode:
use K2gl\Component\Validator\Constraint\EntityExist\ViolationCode; foreach ($validator->validate($dto) as $violation) { if ($violation->getCode() === ViolationCode::ALREADY_EXIST) { // handle "entity already exists" case } if ($violation->getCode() === ViolationCode::NOT_EXIST) { // handle "entity not found" case } }
ViolationCode constants are plain strings that reference the constraint
constants — no duplication, no separate source of truth.