Skip to content

Repository files navigation

Assert an entity exists (or does not) with a Symfony constraint and Doctrine

CILatest Stable VersionTotal DownloadsPHPStan LevelLicense

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

useK2gl\Component\Validator\Constraint\EntityExist\AssertEntityNotExist;
readonlyclass RegisterUserOrder
{
publicfunction__construct(
#[Assert\NotBlank]
#[Assert\Email]
#[AssertEntityNotExist(
entity: User::class,
property: 'email',
message: 'User with email "%value%" already registered.'
)]
publicstring$email,
) {
}
}

AssertEntityExist

useK2gl\Component\Validator\Constraint\EntityExist\AssertEntityExist;
readonlyclass TransferUserToOtherUnitOrder
{
publicfunction__construct(
#[Assert\NotBlank]
#[AssertEntityExist(
entity: User::class,
property: 'uuid',
)]
publicstring$user,
#[Assert\NotBlank]
#[AssertEntityExist(
entity: Unit::class,
property: 'uuid',
)]
publicstring$unit, ) {
}
}

AssertCompositeEntityExist

A class-level constraint for when fields must reference an existing row as a combination — not each field independently. AssertEntityExist on warehouseId and on companyId separately would pass as long as each ID exists somewhere; it can't tell whether the two belong together.

useK2gl\Component\Validator\Constraint\EntityExist\AssertCompositeEntityExist;
#[AssertCompositeEntityExist(
entity: WarehouseItem::class,
fields: ['warehouseId', 'companyId'],
)]
readonlyclass MoveStockOrder
{
publicfunction__construct(
publicstring$warehouseId,
publicstring$companyId,
publicint$quantity,
) {
}
}

The violation is attached to the first field in fields by default; pass errorPath to attach it elsewhere. Validation is skipped if any of the fields is null or an empty string, same as the single-field constraints.

Reads the fields directly off the validated object (public properties, as in the examples above); no extra service wiring needed — the services.yaml snippet above already covers it.

Violation codes

Each constraint declares the violation code it emits as a UUID constant on its own class (AssertEntityExist::NOT_EXIST, AssertEntityNotExist::EXIST, AssertCompositeEntityExist::NOT_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:

useK2gl\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.

Pull requests are always welcome

Collaborate with pull requests

About

Symfony validation constraint that asserts a matching database row exists (or does not), via Doctrine.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages