Symfony + DoctrineExtensions softdeleteable

575 Views Asked by At

i try implement SoftDelete by DoctrineExtensions. But i still get this error:

There is a ForeignKeyConstraintViolationException for the Doctrine entity associated with "Student". Solution: disable the "delete" action for this CRUD controller or configure the "cascade={"remove"}" attribute for the related field in the Doctrine entity. Full exception: An exception occurred while executing a query: SQLSTATE[23503]: Foreign key violation: 7 ERROR: update or delete on table "student" violates foreign key constraint "fk_23fa1e55cb944f1a" on table "order"
DETAIL: Key (id)=(1edc331d-4a67-6454-bbec-09f748d5458b) is still referenced from table "order".

I use traits.

My Enity is:


<?php

namespace App\Entity;

use App\Repository\StudentRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;
use Gedmo\Mapping\Annotation as Gedmo;

#[ORM\Entity(repositoryClass: StudentRepository::class)]
#[Gedmo\SoftDeleteable(fieldName: 'deletedAt', timeAware: false, hardDelete: false)]

class Student implements PersonInterface
{
    use SoftDeleteableEntity;

    #[ORM\Id]
    #[ORM\Column(type: "uuid", unique: true)]
    #[ORM\GeneratedValue(strategy: "CUSTOM")]
    #[ORM\CustomIdGenerator(class: "doctrine.uuid_generator")]
    private ?Uuid $id;

    #[ORM\Column(type: 'string', length: 255, nullable: true, unique: true)]
    #[Groups('student-detail')]
    private string $lectorStudent;
    ....
    ....
    ....

And in config I have

doctrine:
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App
            gedmo_soft_deleteable:
                type: annotation
                prefix: Gedmo\SoftDeleteable\Entity
                dir: '%kernel.project_dir%/src/Entity'
                alias: GedmoSoftDeleteable # optional, but recommended
                is_bundle: false

When I delete the entity, I still delete it using hard delete. Does anyone know where the error is please?

Thank you!

1

There are 1 best solutions below

0
AdiOverRide On

I solved it. I had bad confuration. The right is:

orm:
    auto_generate_proxy_classes: true
    naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
    auto_mapping: true
    filters:
        softdeleteable:
            class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
            enabled: true
    mappings:
        App:
            is_bundle: false
            dir: '%kernel.project_dir%/src/Entity'
            prefix: 'App\Entity'
            alias: App

And then I added doctrine extension with:

services:
  Gedmo\SoftDeleteable\SoftDeleteableListener:
    class: Gedmo\SoftDeleteable\SoftDeleteableListener
    tags:
      - { name: doctrine.event_subscriber, connection: default }
    calls:
      - [ setAnnotationReader, [ "@annotation_reader" ] ]