tourze/doctrine-entity-checker-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

tourze/doctrine-entity-checker-bundle

Composer 安装命令:

composer require tourze/doctrine-entity-checker-bundle

包简介

A Symfony Bundle that provides Doctrine entity checking and processing capabilities with custom checkers, ID generators, and SQL formatting tools.

README 文档

README

English | 中文

Latest Version Total Downloads PHP Version License

A Symfony Bundle that provides Doctrine entity checking and processing capabilities. Through custom checkers, you can automatically handle specific logic before entity persistence.

Table of Contents

Features

  • Automatic entity processing before persistence
  • Support for custom ID generators with #[ORM\CustomIdGenerator] attribute
  • Entity primary key management utilities
  • SQL formatting and generation tools
  • Lazy-loaded services for optimal performance
  • Support for composite primary keys

Installation

Install via Composer:

composer require tourze/doctrine-entity-checker-bundle

Requirements

  • PHP 8.1 or higher
  • Symfony 6.4 or higher
  • Doctrine ORM 3.0 or higher

Quick Start

  1. Register the Bundle
// config/bundles.php
return [
    // ...
    Tourze\DoctrineEntityCheckerBundle\DoctrineEntityCheckerBundle::class => ['all' => true],
];
  1. Create a custom entity checker
<?php

namespace App\Doctrine\Checker;

use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\Persistence\ObjectManager;
use Tourze\DoctrineEntityCheckerBundle\Checker\EntityCheckerInterface;

class TimestampEntityChecker implements EntityCheckerInterface
{
    public function prePersistEntity(ObjectManager $objectManager, object $entity): void
    {
        if (property_exists($entity, 'createTime') && $entity->createTime === null) {
            $entity->createTime = new \DateTimeImmutable();
        }
    }

    public function preUpdateEntity(ObjectManager $objectManager, object $entity, PreUpdateEventArgs $eventArgs): void
    {
        if (property_exists($entity, 'updatedAt')) {
            $entity->updatedAt = new \DateTimeImmutable();
        }
    }
}

The checker will be automatically registered due to the #[AutoconfigureTag] attribute.

Configuration

The bundle works out of the box with minimal configuration. All services are automatically configured with lazy loading for optimal performance.

Service Configuration

All services in this bundle are configured as lazy-loaded by default:

  • EntityChecker - Main entity processing service
  • EntityPrimaryKeyService - Primary key utilities
  • SqlFormatter - SQL generation utilities

These services are automatically injected when needed and support dependency injection.

Custom Configuration

If you need to customize service behavior, you can override services in your config/services.yaml:

services:
    # Override the main entity checker if needed
    Tourze\DoctrineEntityCheckerBundle\Service\EntityChecker:
        # Your custom configuration

Advanced Usage

Custom ID Generator

Use the #[ORM\CustomIdGenerator] attribute to specify custom ID generators:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use App\Generator\SnowflakeIdGenerator;

#[ORM\Entity]
class MyEntity
{
    #[ORM\Id]
    #[ORM\CustomIdGenerator(class: SnowflakeIdGenerator::class)]
    #[ORM\Column(type: 'bigint')]
    private ?int $id = null;

    // ... other properties
}

Primary Key Service

Get primary key information from entities:

<?php

use Tourze\DoctrineEntityCheckerBundle\Service\EntityPrimaryKeyService;

class MyService
{
    public function __construct(
        private EntityPrimaryKeyService $primaryKeyService
    ) {}

    public function analyzeEntity(object $entity): void
    {
        // Get primary key values
        $pkValues = $this->primaryKeyService->getPrimaryKeyValues($entity);
        
        // Check if entity has composite primary key
        $hasComposite = $this->primaryKeyService->hasCompositeIdentifier($entity);
        
        // Get primary key field names
        $fieldNames = $this->primaryKeyService->getIdentifierFieldNames($entity);
    }
}

SQL Formatting

Generate SQL from entities:

<?php

use Tourze\DoctrineEntityCheckerBundle\Service\SqlFormatter;

class MyService
{
    public function __construct(
        private SqlFormatter $sqlFormatter
    ) {}

    public function generateInsertSql(object $entity): array
    {
        // Returns [tableName, parameters]
        [$tableName, $params] = $this->sqlFormatter->getObjectInsertSql(
            $this->entityManager, 
            $entity
        );
        
        // Build INSERT SQL
        $fields = implode(', ', array_keys($params));
        $placeholders = ':' . implode(', :', array_keys($params));
        $sql = "INSERT INTO {$tableName} ({$fields}) VALUES ({$placeholders})";
        
        return [$sql, $params];
    }
}

API Reference

EntityCheckerInterface

  • prePersistEntity(ObjectManager $objectManager, object $entity): void - Called before entity persistence
  • preUpdateEntity(ObjectManager $objectManager, object $entity, PreUpdateEventArgs $eventArgs): void
    • Called before entity update

EntityPrimaryKeyService

  • getPrimaryKeyValues(object $entity): array - Get primary key values
  • hasCompositeIdentifier(string|object $entityClass): bool - Check for composite primary key
  • getIdentifierFieldNames(string|object $entityClass): array - Get primary key field names

SqlFormatter

  • getObjectInsertSql(ObjectManager $objectManager, object $object): array - Generate insert SQL data
  • fromOrmQuery(OrmQuery $query): string - Format DQL with parameters

Testing

Run tests:

# From project root
./vendor/bin/phpunit packages/doctrine-entity-checker-bundle/tests

Run PHPStan analysis:

php -d memory_limit=2G ./vendor/bin/phpstan analyse packages/doctrine-entity-checker-bundle

Contributing

Please see CONTRIBUTING.md for details.

License

The MIT License (MIT). Please see License File for more information.

tourze/doctrine-entity-checker-bundle 适用场景与选型建议

tourze/doctrine-entity-checker-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 40.47k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 03 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 tourze/doctrine-entity-checker-bundle 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 40.47k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 18
  • 依赖项目数: 13
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-24