问题
I have tried to use soft delete (Using gedmo/doctrine-extensions) for some Entities in Symfony 5, and got some troubles:
Listener "SoftDeleteableListener" was not added to the EventManager!
Compile Error: App\Entity\Admin and Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity define the same property ($deletedAt) in the composition of App\Entity\Admin. However, the definition differs and is considered incompatible. Class was composed
This is what I tried, and it runs well
Install gedmo/doctrine-extensions
composer require gedmo/doctrine-extensions
Add column deleted_at to the table what you want to use soft delete (Use migration or add manually)
Add config to config/packages/doctrine.yaml
filters: softdeleteable: class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter enabled: true
Add config to config/services.yaml
gedmo.listener.softdeleteable: class: Gedmo\SoftDeleteable\SoftDeleteableListener tags: - { name: doctrine.event_subscriber, connection: default } calls: - [ setAnnotationReader, [ '@annotation_reader' ] ]
Add Gedmo and use SoftDeleteableEntity to Your Entity
<?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; …. }
And finally, use delete function as usual, the column deleted_at will be updated
/** * @param Admin $admin */ public function delete(Admin $admin) { $this->_em->remove($admin); $this->_em->flush(); }
Note:
Do not need to add deletedAt
field, method getDeletedAt
and setDeletedAt
to Your Entity
来源:https://stackoverflow.com/questions/65895900/symfony-5-including-4-using-gedmo-doctrine-extension-for-softdelete