Extensions to Gedmo's softDeleteable listener which has had this issue reported since 2012 : doctrine-extensions/DoctrineExtensions#505.
Provides the onSoftDelete
functionality to an association of a doctrine entity. This functionality behaves like the SQL onDelete
function (when the owner side is deleted). It will prevent Doctrine errors when a reference is soft-deleted.
This bundle requires Symfony 6.0+ and PHP 8.1+
Add evence/soft-deleteable-extension-bundle to your composer.json
file:
php composer.phar require "evence/soft-deleteable-extension-bundle"
Register bundle into config/bundles.php (Flex did it automatically):
# config/bundles.php
return [
...
new Evence\Bundle\SoftDeleteableExtensionBundle\EvenceSoftDeleteableExtensionBundle(),
];
Cascade delete the entity
To (soft-)delete an entity when its parent record is soft-deleted:
#[Evence\onSoftDelete(type: Type::CASCADE)]
Set reference to null (instead of deleting the entity)
#[Evence\onSoftDelete(type: Type::SET_NULL)]
Replace reference by some property marked as successor (must be of same entity class)
#[Evence\onSoftDelete(type: Type::SUCCESSOR)]
<?php
declare(strict_types=1);
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Evence\Bundle\SoftDeleteableExtensionBundle\Mapping\Attribute as Evence;
#[ORM\Entity(repositoryClass: 'AppBundle\Entity\AdvertisementRepository')]
#[Gedmo\SoftDeleteable(fieldName: 'deletedAt')]
class Advertisement
{
...
#[Evence\onSoftDelete(type: Type::CASCADE)]
#[ORM\ManyToOne(targetEntity: 'App\Entity\Shop')]
#[ORM\JoinColumn(nullable: false)]
private $shop;
...
}