PHPackages                             k2gl/entity-exist - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. k2gl/entity-exist

ActiveSymfony-bundle[Database &amp; ORM](/categories/database)

k2gl/entity-exist
=================

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

1.15.0(today)11.6k1MITPHPPHP ^8.1CI passing

Since Oct 20Pushed 2w ago1 watchersCompare

[ Source](https://github.com/k2gl/entity-exist)[ Packagist](https://packagist.org/packages/k2gl/entity-exist)[ RSS](/packages/k2gl-entity-exist/feed)WikiDiscussions main Synced today

READMEChangelog (1)Dependencies (50)Versions (20)Used By (1)

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

[](#assert-an-entity-exists-or-does-not-with-a-symfony-constraint-and-doctrine)

[![CI](https://camo.githubusercontent.com/25053a1da14958593e8976be4418d92212e74af0c544b8b30f2f896c9a559be1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6b32676c2f656e746974792d65786973742f63692e796d6c3f6272616e63683d6d61696e266c6162656c3d4349266c6f676f3d676974687562)](https://github.com/k2gl/entity-exist/actions/workflows/ci.yml)[![Latest Stable Version](https://camo.githubusercontent.com/b7caa14f05887795ea8166f96936b830ee43731456b34815dddc1f2a6a6a58f7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6b32676c2f656e746974792d65786973743f6c6f676f3d7061636b6167697374266c6f676f436f6c6f723d7768697465)](https://packagist.org/packages/k2gl/entity-exist)[![Total Downloads](https://camo.githubusercontent.com/f845022d4ed421c2830310fd34cbd0f0f9645e8916740ba16f048eb45d6cb0a8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6b32676c2f656e746974792d65786973743f6c6f676f3d7061636b6167697374266c6f676f436f6c6f723d7768697465)](https://packagist.org/packages/k2gl/entity-exist)[![PHPStan Level](https://camo.githubusercontent.com/01c58e66f2fafb70c17613ff2b1da3f549aade3a735b076da5cd9e5c04b945a5/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230392d3261356561373f6c6f676f3d706870266c6f676f436f6c6f723d7768697465)](https://phpstan.org)[![License](https://camo.githubusercontent.com/3ae763d53d771a77e7c557834eb3782ae6a1268cb1f5a3acdea066b8cb2394c4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6b32676c2f656e746974792d65786973743f636f6c6f723d79656c6c6f77677265656e)](https://packagist.org/packages/k2gl/entity-exist)

Requirements
------------

[](#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
------------

[](#installation)

You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/):

```
composer require k2gl/entity-exist

```

Configuration
-------------

[](#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
-----

[](#usage)

### AssertEntityNotExist

[](#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

[](#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,
    ) {
    }
}
```

### AssertCompositeEntityExist

[](#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.

```
use K2gl\Component\Validator\Constraint\EntityExist\AssertCompositeEntityExist;

#[AssertCompositeEntityExist(
    entity: WarehouseItem::class,
    fields: ['warehouseId', 'companyId'],
)]
readonly class MoveStockOrder
{
    public function __construct(
        public string $warehouseId,
        public string $companyId,
        public int $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
---------------

[](#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`:

```
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.

Pull requests are always welcome
--------------------------------

[](#pull-requests-are-always-welcome)

[Collaborate with pull requests](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request)

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance98

Actively maintained with recent releases

Popularity22

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~75 days

Recently: every ~200 days

Total

19

Last Release

0d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6bc4aa529c7f13ea593297497f6eae20d5c07f476baa0a551960d7e6ff1e5413?d=identicon)[k2gl](/maintainers/k2gl)

---

Top Contributors

[![k2gl](https://avatars.githubusercontent.com/u/2846079?v=4)](https://github.com/k2gl "k2gl (33 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/k2gl-entity-exist/health.svg)

```
[![Health](https://phpackages.com/badges/k2gl-entity-exist/health.svg)](https://phpackages.com/packages/k2gl-entity-exist)
```

###  Alternatives

[easycorp/easyadmin-bundle

Admin generator for Symfony applications

4.3k17.9M388](/packages/easycorp-easyadmin-bundle)[rcsofttech/audit-trail-bundle

Enterprise-grade, high-performance Symfony audit trail bundle. Automatically track Doctrine entity changes with split-phase architecture, multiple transports (HTTP, Queue, Doctrine), and sensitive data masking.

1189.8k](/packages/rcsofttech-audit-trail-bundle)[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.5k5.9M733](/packages/sylius-sylius)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.4M203](/packages/sulu-sulu)[kimai/kimai

Kimai - Time Tracking

4.8k9.0k1](/packages/kimai-kimai)[open-dxp/opendxp

Content &amp; Product Management Framework (CMS/PIM)

9421.6k61](/packages/open-dxp-opendxp)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
