定制 chrisbaltazar/doctrine-soft-delete 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

chrisbaltazar/doctrine-soft-delete

Composer 安装命令:

composer require chrisbaltazar/doctrine-soft-delete

包简介

Symfony Bundle to handle Soft deleted records with Doctrine

README 文档

README

A Symfony Bundle to handle soft-deleted records transparently with Doctrine ORM and MySQL.

Instead of permanently deleting rows, soft-delete marks records with a deleted_at timestamp and automatically excludes them from all queries via a Doctrine SQL filter.

PHP >= 8.1 License: MIT

Features

  • ✅ Automatic deleted_at column filtering via a Doctrine SQL filter
  • ✅ MySQL GENERATED column (auto_generated_active_flag) for safe unique indexes on soft-deletable tables
  • ✅ PHP 8 attribute #[SoftDeleteUniqueIndex] to declare unique constraints that respect soft deletes
  • ✅ Custom MySQL schema comparator that prevents unnecessary migration noise
  • ✅ Zero-config autowiring via Symfony's service container

Compatibility

  • Symfony 6.4+
  • Doctrine ORM 3.6+
  • MySQL 5.7+ (for generated columns)

Dependencies

Dependency Version
PHP >= 8.1
symfony/framework-bundle ^6.4
doctrine/doctrine-bundle ^2.18
doctrine/orm ^3.6
doctrine/doctrine-migrations-bundle ^3.7

Installation

composer require chrisbaltazar/doctrine-soft-delete

Please make sure the bundle gets registered in your config/bundles.php: (normally handled by Symfony Flex)

return [
    // ...
    Database\SoftDelete\SoftDeleteBundle::class => ['all' => true],
];

Usage

To auto enable the soft delete function, simply implement the SoftDeletableInterface in your entity and add a nullable deletedAt property:

use Database\SoftDelete\Core\Contract\SoftDeletableInterface;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class Product implements SoftDeletableInterface
{
    #[ORM\Column(nullable: true)]
    private ?\DateTimeImmutable $deletedAt = null;

    public function setDeletedAt(\DateTimeImmutable $deletedAt): void
    {
        $this->deletedAt = $deletedAt;
    }

    public function getDeletedAt(): ?\DateTimeImmutable
    {
        return $this->deletedAt;
    }
}

Then just remember to update your entities accordingly when you want to soft-delete a record:

$product->setDeletedAt(new \DateTimeImmutable());
$entityManager->flush();

This will automatically exclude soft-deleted records from all queries.

Temporarily include soft-deleted records

To include them, you can disable the filter at any point during your request flow with:

$entityManager->getFilters()->disable('soft_delete');
... 
$users = $entityManager->getRepository(User::class)->findAll(); // includes soft-deleted records

Unique soft-deletable records

For unique items that should ignore soft-deleted records, use the #[SoftDeleteUniqueIndex] attribute: This will create an auto-generated column in your entity table as a boolean flag + a table index, that would control the uniqueness behavior of the specified fields, ignoring soft-deleted records (where deleted_at is not null).

use Database\SoftDelete\Core\Attribute\SoftDeleteUniqueIndex;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[SoftDeleteUniqueIndex(fields: ['email'])]
class User implements SoftDeletableInterface
{
    // ...
}

After that just generate a new migration as normal and execute it to update your database schema.

Then simply handle the unique constraint violation properly as the following example:

use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
... 
// Perform form validation and any other necessary checks before attempting to persist the user entity
...
 
try {
    $entityManager->persist($user);
    $entityManager->flush();
} catch (UniqueConstraintViolationException) {
    $this->addFlash('error', 'A user with this email already exists.');
}

// Continue with the rest of your controller logic, such as rendering a response or redirecting the user

Demo

A demo Symfony application is available in the demo/ directory of this repository, showcasing the bundle's features in action. To run the demo locally just use the handy commands available in the Makefile:

(Make sure you have already a docker environment well configured with docker-compose and Make installed on your machine)

make demo-run

After that you should be able to access the demo app at http://localhost:8000 and see how soft-deleted records are handled in the UI. (the port number can be also customized in the docker-compose.override.yml file if needed)

Then just go over: http://localhost:8000/user and try it out as any other CRUD 🚀

Testing

To run the tests, simply execute:

make test

This will run all PHPUnit tests defined in the tests/ directory, ensuring that the bundle's functionality is working as expected, ensuring the special order and conditions needed to properly test the database custom components and the generated column behavior.

chrisbaltazar/doctrine-soft-delete 适用场景与选型建议

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

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

围绕 chrisbaltazar/doctrine-soft-delete 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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