我尝试对 Symfony 5 中的某些实体使用软删除(使用 gedmo/doctrine-extensions),但遇到了一些麻烦:
侦听器“SoftDeleteableListener”未添加到 EventManager!
编译错误:App\Entity\Admin 和 Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity 在 App\Entity\Admin 的组合中定义了相同的属性 ($deletedAt)。然而,定义不同并且被认为是不兼容的。班级组成
这是我尝试过的,运行良好
-
安装 gedmo/doctrine-extensions
composer require gedmo/doctrine-extensions
-
将列deleted_at添加到要使用软删除的表中(使用迁移或手动添加)
-
将配置添加到 config/packages/doctrine.yaml
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
-
将配置添加到 config/services.yaml
gedmo.listener.softdeleteable:
class: Gedmo\SoftDeleteable\SoftDeleteableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ '@annotation_reader' ] ]
-
添加 Gedmo 并在您的实体中使用 SoftDeleteableEntity
<?php
namespace App\Entity;
use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;
/**
* @ORM\Entity(repositoryClass=AdminRepository::class)
* @ORM\Table(name="admins")
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false,
hardDelete=false)
*/
class Admin implements UserInterface
{
use SoftDeleteableEntity;
….
}
-
最后,照常使用删除功能,deleted_at 列将被更新
/**
* @param Admin $admin
*/
public function delete(Admin $admin)
{
$this->_em->remove($admin);
$this->_em->flush();
}
Note:
不需要添加deletedAt
领域、方法getDeletedAt
and setDeletedAt
给您的实体
如果您使用 PHP 格式 (config/services.php) 而不是 YAML,请将步骤 4 更改为以下内容。
$services->set("gedmo.listener.softdeleteable")
->class(\Gedmo\SoftDeleteable\SoftDeleteableListener::class)
->tag(name: 'doctrine.event_subscriber', attributes: ["method" => "setAnnotationReader", "connection" => "default"])
->args([service('doctrine.orm.metadata.annotation_reader')])
;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)