Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3927

Symfony 5 (Including 4) using Gedmo Doctrine Extension for SoftDelete

$
0
0

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

  1. Install gedmo/doctrine-extensions

     composer require gedmo/doctrine-extensions
  2. Add column deleted_at to the table what you want to use soft delete (Use migration or add manually)

  3. Add config to config/packages/doctrine.yaml

     filters:     softdeleteable:     class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter     enabled: true
  4. Add config to config/services.yaml 

     gedmo.listener.softdeleteable:     class: Gedmo\SoftDeleteable\SoftDeleteableListener     tags:         - { name: doctrine.event_subscriber, connection: default }     calls:         - [ setAnnotationReader, [ '@annotation_reader' ] ]
  5. 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;…. }
  6. 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


Viewing all articles
Browse latest Browse all 3927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>