phpsymfonydoctrine-ormdoctrinegedmo-tree

Accessing to Gedmo soft deleted entity


In annotations for the class of User entity, I have:

@Gedmo\SoftDeleteable(fieldName="deletedAt", hardDelete=false)

And later in code:

/**
 * @var \DateTime
 * @ORM\Column(type="datetime", nullable=true)
 */
protected $deletedAt;

Soft delete works fine, but I want modification so soft deleted User still can be accessed, for example with $message->getSentBy(). Because User foreign key is in another table.

In class Message I have the field:

/**
 * @ORM\ManyToOne(targetEntity="User")
 * @ORM\JoinColumn(name="locked_by", referencedColumnName="id")
 */
private $sentBy;

And now because of soft delete, wherever regular active Users want to see messages sent by soft deleted User I get an error invalid user (the User entity is not found).

Is there any way to modify soft delete so it is selectable on places where another foreign key uses that field? Or to be available to use on select statements.

I am using Symfony 5.4 on PHP 7.4


Solution

  • Just disable the filter when you need to access "deleted" entities:

    $em->getFilters()->disable('soft-deleteable');
    $art = $repo->findOneBy(array('title' => 'My Article'));
    
    // or
    // Enable / Disable filter filter, for specified entity (default is enabled for all)
    $filter = $em->getFilters()->enable('soft-deleteable');
    $filter->disableForEntity('Entity\Article');
    $filter->enableForEntity('Entity\Article');
    

    see https://github.com/doctrine-extensions/DoctrineExtensions/blob/main/doc/softdeleteable.md#usage