承接 k2gl/entity-exist 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

k2gl/entity-exist

Composer 安装命令:

composer require k2gl/entity-exist

包简介

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

README 文档

README

CI Latest Stable Version Total Downloads PHPStan Level License

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,        
    ) {
    }
}

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

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

Collaborate with pull requests

k2gl/entity-exist 适用场景与选型建议

k2gl/entity-exist 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.65k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 10 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 k2gl/entity-exist 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 k2gl/entity-exist 我们能提供哪些服务?
定制开发 / 二次开发

基于 k2gl/entity-exist 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.65k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 12
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-10-20