Symfony 5包括 4 个使用 Gedmo Doctrine Extension for SoftDelete

问题描述

我曾尝试对 Symfony 5 中的某些实体使用软删除(使用 gedmo/doctrine-extensions),但遇到了一些麻烦:

侦听器“SoftDeleteableListener”未添加到 EventManager!

编译错误:App\Entity\Admin 和 Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity 在 App\Entity\Admin 的组合中定义了相同的属性($deletedAt)。但是,定义不同并且被认为是不兼容的。班级组成

这是我试过的,运行良好

  1. 安装gedmo/doctrine-extensions

     composer require gedmo/doctrine-extensions
    
  2. 将要使用软删除的列添加到表中删除(使用迁移或手动添加

  3. 将配置添加到 config/packages/doctrine.yaml

     filters:
    
         softdeleteable:
    
         class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
    
         enabled: true
    
  4. 将配置添加到 config/services.yaml

     gedmo.listener.softdeleteable:
         class: Gedmo\SoftDeleteable\SoftDeleteableListener
         tags:
             - { name: doctrine.event_subscriber,connection: default }
         calls:
             - [ setAnnotationReader,[ '@annotation_reader' ] ]
    
  5. 向您的实体添加 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;
         ….
     }
    
  6. 最后,照常使用delete函数,deleted_at列将被更新

     /**
      * @param Admin $admin
      */
      public function delete(Admin $admin)
     {
         $this->_em->remove($admin);
         $this->_em->flush();
     }
    

注意: 不需要将 deletedAt 字段、方法 getDeletedAtsetDeletedAt 添加到您的实体

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)