stichtingsd/软删除扩展包

1.0.3 2023-06-05 13:37 UTC

This package is auto-updated.

Last update: 2024-09-05 16:41:27 UTC


README

警告:此扩展包仅适用于 Symfony ^6.0 和 PHP ^8.1

此扩展包旨在处理与 Gedmo SoftDelete 功能相结合的关联实体/文档的软删除。Gedmo 不处理级联关系,例如。Doctrine 也不处理,因为它实际上并没有被删除。

由于 Doctrine DQL 和代理对象,此扩展包还非常擅长处理大量实体的删除。具有 10,000+ 关系的实体可以在一秒内更新。

先决条件

此扩展包需要 Symfony ^6.1 或更高版本和 PHP ^8.1 或更高版本

  • Symfony ^6.0
  • PHP ^8.1
  • doctrine/doctrine-extensions ^2.0 | ^3.0 (Gedmo)

安装

将 stichtingsd/软删除扩展包添加到您的 composer.json 文件

composer require "stichtingsd/soft-deleteable-extension-bundle"

包注册(当使用 Symfony Flex 时跳过)

将包注册到 config/bundles.php 中(Flex 会自动完成此操作)

# config/bundles.php

return [
    ...
    new StichtingSD\SoftDeleteableExtensionBundle\StichtingSDSoftDeleteableExtensionBundle(),
];

入门指南

级联软删除实体

当父记录软删除时删除实体

use StichtingSD\SoftDeleteableExtensionBundle\Mapping\Attribute as StichtingSD;
use StichtingSD\SoftDeleteableExtensionBundle\Mapping\Type;

...

#[StichtingSD\onSoftDelete(type: Type::CASCADE)]

将引用设置为 null(而不是删除实体)

use StichtingSD\SoftDeleteableExtensionBundle\Mapping\Attribute as StichtingSD;
use StichtingSD\SoftDeleteableExtensionBundle\Mapping\Type;

...

#[StichtingSD\onSoftDelete(type: Type::SET_NULL)]

支持的关联类型

  • 一对一
    • 类型::级联
    • 类型::SET_NULL
  • 多对一
    • 类型::级联
    • 类型::SET_NULL
  • 多对多
    • 类型::REMOVE_ASSOCIATION_ONLY

类型::级联

这类似于 SQL CASCADE。所有具有 Gedmo 软删除字段的关联都将被删除。

类型::SET_NULL

这类似于 SQL SET_NULL。关联将设置为 NULL。
注意:您的字段必须设置为 nullable: true 以允许空值。

类型::REMOVE_ASSOCIATION_ONLY

这将仅删除关联。关联实体本身不会被删除,因为它们可以有其他关联。

完整示例:多对一

<?php

declare(strict_types=1);

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use StichtingSD\SoftDeleteableExtensionBundle\Mapping\Attribute as StichtingSD;
use StichtingSD\SoftDeleteableExtensionBundle\Mapping\Type;
use App\Entity\Tenant;

#[ORM\Entity(repositoryClass: 'App\Repository\UserRepository')]
#[Gedmo\SoftDeleteable(fieldName: 'deletedAt')]
class User
{
    ...

    #[StichtingSD\onSoftDelete(type: Type::CASCADE)]
    #[ORM\ManyToOne(targetEntity: Tenant::class)]
    #[ORM\JoinColumn(nullable: false)]
    private ?Tenant $tenant;

    ...
}

完整示例:多对多(映射侧)

<?php

declare(strict_types=1);

namespace App\Entity;

use App\Entity\Tenant;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Gedmo\Mapping\Annotation as Gedmo;
use StichtingSD\SoftDeleteableExtensionBundle\Mapping\Attribute as StichtingSD;
use StichtingSD\SoftDeleteableExtensionBundle\Mapping\Type;

#[ORM\Entity(repositoryClass: 'App\Repository\UserRepository')]
#[Gedmo\SoftDeleteable(fieldName: 'deletedAt')]
class User
{
    ...

    #[StichtingSD\onSoftDelete(type: Type::REMOVE_ASSOCIATION_ONLY)]
    #[ORM\ManyToMany(targetEntity: Tenant::class, inversedBy: 'users')]
    private Collection $tenants;

    ...
}

完整示例:多对多(反向侧)

<?php

declare(strict_types=1);

namespace App\Entity;

use App\Entity\User;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use StichtingSD\SoftDeleteableExtensionBundle\Mapping\Attribute as StichtingSD;
use StichtingSD\SoftDeleteableExtensionBundle\Mapping\Type;

#[ORM\Entity(repositoryClass: 'App\Repository\TenantRepository')]
#[Gedmo\SoftDeleteable(fieldName: 'deletedAt')]
class Tenant
{
    ...

    #[StichtingSD\onSoftDelete(type: Type::REMOVE_ASSOCIATION_ONLY)]
    #[ORM\ManyToMany(targetEntity: User::class, mappedBy: 'tenants')]
    private Collection $users;

    ...
}

限制

多对多

  • 目前,只能使用 Type::REMOVE_ASSOCIATION_ONLY

一对一

  • 不支持,也不会支持。在多对一侧定义它。