https://github.com/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
https://github.com/k2gl/entity-exist
Last synced: 3 months ago
JSON representation
Checks if there is at least one row with the required field in the database, for example, a row with the same identifier
- Host: GitHub
- URL: https://github.com/k2gl/entity-exist
- Owner: k2gl
- License: mit
- Created: 2022-10-20T22:20:47.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-04-21T16:29:35.000Z (over 2 years ago)
- Last Synced: 2025-01-17T05:29:22.463Z (over 1 year ago)
- Language: PHP
- Size: 69.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Assert entity exist or not using Symfony constraints and Doctrine.
[](https://github.com/k2gl/entity-exist/actions?workflow=CI)
## 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
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
```php
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
```php
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,
) {
}
}
```
## 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)