zhortein/doctrine-lifecycle-bundle 问题修复 & 功能扩展

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

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

zhortein/doctrine-lifecycle-bundle

Composer 安装命令:

composer require zhortein/doctrine-lifecycle-bundle

包简介

Reusable Doctrine lifecycle features for Symfony entities, including timestampable and blameable support.

README 文档

README

Reusable Doctrine lifecycle features for Symfony entities.

This bundle provides a clean and focused foundation for common entity lifecycle concerns in Symfony applications using Doctrine ORM.

It currently includes:

  • Timestampable support
  • Blameable support
  • Doctrine lifecycle listeners based on attributes
  • UTC-based lifecycle timestamps

The bundle is designed to stay small, explicit and reusable.

Features

Timestampable

Automatic handling of:

  • createdAt
  • updatedAt

Blameable

Automatic handling of:

  • createdByIdentifier
  • updatedByIdentifier

Blameable values are stored as scalar identifiers, which keeps the bundle generic and independent from any application-specific User entity.

Doctrine integration

The bundle uses Doctrine listeners to update lifecycle fields automatically on:

  • prePersist
  • preUpdate

UTC timestamps

Lifecycle dates are generated in UTC to provide predictable and consistent storage across applications.

Current Scope

This bundle focuses only on entity lifecycle metadata.

It does not try to handle:

  • audit trail history
  • workflow/state machines
  • publication systems
  • translations
  • business-specific ownership models

If you need full audit logging, use a dedicated audit bundle alongside this one.

Requirements

  • PHP 8.4+
  • Symfony 7.4 or 8.0
  • Doctrine Bundle
  • Doctrine ORM

Installation

Composer

composer require zhortein/doctrine-lifecycle-bundle

Bundle registration

If Symfony Flex does not register the bundle automatically, add it manually in config/bundles.php:

<?php

return [
    // ...
    Zhortein\DoctrineLifecycleBundle\ZhorteinDoctrineLifecycleBundle::class => ['all' => true],
];

Default Behavior

Timestampable

On prePersist:

  • createdAt is set if it is currently null
  • updatedAt is always set

On preUpdate:

  • updatedAt is updated

Blameable

On prePersist:

  • createdByIdentifier is set if it is currently null
  • updatedByIdentifier is always set

On preUpdate:

  • updatedByIdentifier is updated

If no actor can be resolved, blameable fields remain unchanged.

Usage

Timestampable

1. Implement the interface and use the trait

<?php

declare(strict_types=1);

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Zhortein\DoctrineLifecycleBundle\Contract\TimestampableInterface;
use Zhortein\DoctrineLifecycleBundle\Trait\TimestampableTrait;

#[ORM\Entity]
final class Destination implements TimestampableInterface
{
    use TimestampableTrait;

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private string $name;

    public function __construct(string $name)
    {
        $this->name = $name;
    }
}

2. Result

The bundle will automatically maintain:

  • createdAt
  • updatedAt

Blameable

1. Implement the interface and use the trait

<?php

declare(strict_types=1);

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Zhortein\DoctrineLifecycleBundle\Contract\BlameableInterface;
use Zhortein\DoctrineLifecycleBundle\Trait\BlameableTrait;

#[ORM\Entity]
final class Article implements BlameableInterface
{
    use BlameableTrait;

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private string $title;

    public function __construct(string $title)
    {
        $this->title = $title;
    }
}

2. Result

The bundle will automatically maintain:

  • createdByIdentifier
  • updatedByIdentifier

These values are provided by an ActorResolverInterface.

Timestampable + Blameable together

A single entity can use both features:

<?php

declare(strict_types=1);

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Zhortein\DoctrineLifecycleBundle\Contract\BlameableInterface;
use Zhortein\DoctrineLifecycleBundle\Contract\TimestampableInterface;
use Zhortein\DoctrineLifecycleBundle\Trait\BlameableTrait;
use Zhortein\DoctrineLifecycleBundle\Trait\TimestampableTrait;

#[ORM\Entity]
final class ExpertProfile implements TimestampableInterface, BlameableInterface
{
    use TimestampableTrait;
    use BlameableTrait;

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private string $displayName;

    public function __construct(string $displayName)
    {
        $this->displayName = $displayName;
    }
}

Actor Resolver

The bundle relies on the following contract:

<?php

declare(strict_types=1);

namespace Zhortein\DoctrineLifecycleBundle\Resolver;

interface ActorResolverInterface
{
    public function resolveActorIdentifier(): ?string;
}

By default, the bundle provides a null resolver, which means blameable fields remain null unless you override the service.

Using Symfony Security as actor resolver

In most applications, you will want to resolve the actor identifier from the authenticated user.

Example:

<?php

declare(strict_types=1);

namespace App\Security;

use Symfony\Bundle\SecurityBundle\Security;
use Zhortein\DoctrineLifecycleBundle\Resolver\ActorResolverInterface;

final class SecurityActorResolver implements ActorResolverInterface
{
    public function __construct(
        private readonly Security $security,
    ) {
    }

    public function resolveActorIdentifier(): ?string
    {
        $user = $this->security->getUser();

        if (null === $user) {
            return null;
        }

        if (!method_exists($user, 'getUserIdentifier')) {
            return null;
        }

        return $user->getUserIdentifier();
    }
}

Then override the resolver binding in your application:

services:
    App\Security\SecurityActorResolver: ~

    Zhortein\DoctrineLifecycleBundle\Resolver\ActorResolverInterface:
        alias: App\Security\SecurityActorResolver

Notes about timestamps

This bundle stores lifecycle timestamps as:

  • \DateTimeImmutable
  • Doctrine datetime_immutable
  • generated in UTC

This is intentional.

The bundle handles technical lifecycle timestamps, not application-level timezone presentation.
If your application needs to display dates in a specific timezone, convert them at application level.

Example:

$localDate = $entity->getUpdatedAt()?->setTimezone(new \DateTimeZone('Europe/Paris'));

Philosophy

This bundle intentionally follows a few rules:

  • keep the scope narrow
  • avoid application-specific assumptions
  • stay independent from the host application's User entity
  • prefer predictable behavior over excessive magic
  • make integration simple in real Symfony projects

Development Status

The bundle is already usable for:

  • timestampable metadata
  • blameable metadata
  • real integration into Symfony applications

Additional lifecycle helpers may come later, but only if they remain aligned with the bundle's narrow scope.

Roadmap

Possible future additions:

  • soft delete metadata support
  • optional configuration improvements
  • additional integration tests
  • more documentation and recipes

Quality Assurance

This bundle includes:

  • PHPUnit tests
  • PHPStan configuration
  • PHP-CS-Fixer configuration
  • CI workflow

Typical local commands:

make csfixer
make phpstan
make test

Testing locally with a path repository

When developing the bundle alongside a Symfony project, you can use a Composer path repository in the host application:

{
  "repositories": [
    {
      "type": "path",
      "url": "../doctrine-lifecycle-bundle",
      "options": {
        "symlink": true
      }
    }
  ],
  "require": {
    "zhortein/doctrine-lifecycle-bundle": "*@dev"
  }
}

This allows you to test the bundle directly in a real application without publishing it first.

License

This bundle is released under the MIT License.

zhortein/doctrine-lifecycle-bundle 适用场景与选型建议

zhortein/doctrine-lifecycle-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 04 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「bundle」 「timestampable」 「doctrine」 「Blameable」 「lifecycle」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-19