i-kadar/cascading-deletion

该软件包最新版本(v1.0.4)没有提供许可信息。

提供级联删除服务的软件包

v1.0.4 2023-12-12 15:38 UTC

This package is auto-updated.

Last update: 2024-09-19 13:05:44 UTC


README

概述

级联删除服务旨在管理和执行系统内实体的删除,侧重于实体间的相互依赖性。系统结构确保数据完整性,通过全面检查主实体和依赖实体的删除资格,并管理跨相关实体的删除级联效果。

核心组件

1. CascadingDeletionService (CascadingDeletionService.php)

目的:管理级联删除过程的启动和协调。为在系统内删除实体提供简单的接口。

关键功能

  • 实体删除处理:管理实体的删除,确保主实体和依赖实体的资格。
  • 数据完整性:在删除之前进行全面的检查,如果删除链中的任何实体无法删除,则中止进程。
  • 删除过程执行:收集所有要删除的实体,验证其资格,如果所有实体都合格,则执行删除过程。

2. Entity Repository (EntityRepository.phpEntityRepositoryInterface.php)

EntityRepository:实现 EntityRepositoryInterface 的抽象类。提供管理实体删除的核心逻辑。子类实现抽象方法以处理实体间特定的关系。EntityRepositoryInterface:定义仓库操作的约定。概述管理删除目标的基本方法。

关键功能

  • 参考实体检索:识别和检索由给定实体引用的实体,管理实体关系和依赖关系。包括抽象方法 getReferencedDeletionTargetsgetReferencedDeletionTargets。这些方法在子类中实现以处理实体间特定的关系。
  • 可删除性检查:确定给定实体是否可以删除,包括抽象方法 getReferencedDeletionTargetscheckDeletability
  • 不可更改的核心逻辑:通过将关键方法声明为 final,确保级联删除逻辑在整个系统中的一致性和完整性。

不可更改的核心逻辑

  • 最终方法:以下关键方法被声明为 final,以保留级联删除过程的核心逻辑,并确保整个系统中的级联删除逻辑的一致性和完整性
    • final public function getUnDeletableTargets(int $entityId, array $deletionTargets): array
    • final public function addTargetToUndeletables(DeletionTargetInterface $target, array $undeletableTargets): void

3. Deletion Targets (DeletionTargetInterface.phpDeletionTarget.php)

DeletionTargetInterface:概述管理删除目标的基本方法。DeletionTarget:实现 DeletionTargetInterface,代表系统中的实体。

属性和方法

  • 实体识别:包括获取实体 ID 和相关仓库的方法。
  • 删除状态管理:设置/获取 isDeletable 状态和解释删除状态的消息的方法。
  • 状态跟踪:包含 checkingStarted 属性以跟踪删除资格检查的状态,这对于管理依赖关系至关重要。包括设置/获取检查状态的方法。

系统功能

该系统专为处理复杂的实体关系而定制,尤其关注在删除一个实体时触发相关实体删除的场景。在EntityRepository中将某些方法指定为final强调了系统对保持一致和健壮删除过程的承诺。这种级联删除过程被精心设计,以维护数据完整性,高效处理依赖关系,并避免依赖检查中的无限循环等问题。

CascadingDeletionService::deleteEntity 方法的使用示例

实现基本仓库类的示例

以下示例演示了如何创建一个简单的仓库类,该类扩展了抽象的EntityRepository。该类负责在级联删除的上下文中管理特定类型的实体。

BasicEntityRepository 示例

class BasicEntityRepository extends EntityRepository {

    // Implementing the abstract method from EntityRepository
    public function getReferencedDeletionTargets(int $entityId): array {
        // Example logic: Retrieve IDs of entities referenced by the entity with the given ID
        // This is typically where you would query your database or data source
        // For simplicity, we're returning a static array of sample data
        return [
            new DeletionTarget(101, $this), // Assuming entity ID 101 is a dependent entity
            new DeletionTarget(102, $this)  // Assuming entity ID 102 is another dependent entity
        ];
    }

    // Implementing the abstract method from EntityRepository
    public function checkDeletability(DeletionTargetInterface $target, array $targets): bool {
        // Example logic: Check if the entity is deletable
        // This might involve checking certain conditions or rules
        // Returning true for simplicity
        return true;
    }

    // Implementing the abstract method from EntityRepository
    public function performDeletion(int $entityId): void {
        // Example logic: Perform the actual deletion of the entity
        // This is where you would typically delete the entity from your database or data source
        echo "Deleting entity with ID: " . $entityId . "\n";
    }
}

示例:从 BasicEntityRepository 中删除实体

$cascadingDeletionService = new CascadingDeletionService();
$basicEntityRepository = new BasicEntityRepository();
$entity = new DeletionTarget(200, $basicEntityRepository);
list($deletionSuccessful, $undeletableEntities) = $cascadingDeletionService->deleteEntity($entity);

if ($deletionSuccessful) {
    echo "Entity was deleted successfully.\n";
} else {
    echo "Deletion was aborted due to undeletable dependents.\n";
    echo "Undeletable entities: \n";
    foreach ($undeletableEntities as $undeletableEntity) {
        echo "Entity ID: " . $undeletableEntity->getEntityId() . "\n";
        echo "Repository: " . get_class($undeletableEntity->getRepository()) . "\n";
        echo "Message: " . $undeletableEntity->getMessage() . "\n";
    }
}