7

I'm building a Symfony2 project and am using gedmo/doctrine-extensions (GitHub) to implement soft delete. My question is whether there's a way to "disable" or "override" softdelete, or even detect if something has been soft deleted.

Here's the situation:

I have a "note" entity that references a "user" entity. A specific note references a user that has been soft deleted. Even though the user has been deleted, it returns true for TWIG's "is defined" logic and can even return the id of the deleted user. However, if I query for any other information (including the "deletedAt" parameter that marks whether or not it is been deleted) I get a 500 "Entity was not found" error.

Since the data is actually still there, and since the note itself hasn't been deleted, I'd still like to say who's written the note, even though the user has been deleted.

Is that possible? If not, how do I properly detect whether something has been soft deleted? Like I said, $note->getUser() still retrieves an object and returns true for any null / "is defined" comparisons.

1

4 Answers 4

15

You can do this by :

$filter = $em->getFilters()->enable('soft-deleteable');
$filter->disableForEntity('Entity\User');
$filter->enableForEntity('Entity\Note');
1
  • 3
    It can be 'softdeleteable' instead of 'soft-deleteable' ... (i'm using stof doctrine extensions)
    – eDoV
    Sep 25, 2015 at 10:21
7

You need to set the relationship loading to eager, this will prevent lazy loading of objects with just an id and nothing else.

You can find more information on eager loading and it's annotation here:

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#by-eager-loading

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html.

As for my code, this is how it looks like when defining a link to a User now:

/**
 * @ORM\ManyToOne(targetEntity="User", inversedBy="answers", fetch="EAGER")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
private $user;

In this case, the User entity can have multiple answers. When loading a User from the answer perspective, this will work:

foreach($answers as $answer) {
    $user = $answer->getUser();

    if (!$user) {
        continue;
    }
}
0
1

You can temporarily disable soft-delete so that deleted items are returned in your results. See the documentation, specifically interesting for you is the section that reads:

This will disable the SoftDeleteable filter, so entities which were "soft-deleted" will appear in results $em->getFilters()->disable('soft-deleteable');

So, first run the code above on your Entity Manager $em and then use it to collect your $note.

1
  • The problem is that I don't want parent entities (i.e. notes) that have been soft deleted. Disabling the filter will also return deleted notes which I don't want. Oct 24, 2013 at 11:41
0

I made a helper function for this so the only thing I have to write is this:

$softDeleted = GedmoSoftDeletableUtils::getSoftDeletedFor(
    MyClass:class, // the class for the soft-deleted items
    $em, // Pass an EntityManager
    // Optionally pass some extra Criteria if you want:
    [Criteria::expr()->eq('someExtraField', 'someValue')]
);

This is the general helper function, just put it somewhere as a Utility class

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityManagerInterface;

class GedmoSoftDeletableUtils
{    
    public static function getSoftDeletedFor(
        string $class,
        EntityManagerInterface $em,
        ?array $extraExpressions = []
    ): ArrayCollection {
        // First disable the soft-deleted filter for this entity
        $em->getFilters()->getFilter('softdeleteable')->disableForEntity($class);

        // The base criteria for getting the soft-deleted elements
        $criteria = (new Criteria())->andWhere(Criteria::expr()->neq('deletedAt', null));

        foreach ($extraExpressions as $expression) {
            $criteria->andWhere($expression);
        }

        // Trigger the LazyCriteriaCollection with toArray() before enabling the "softDeletableFilter" again
        $result = new ArrayCollection(
            $em->getRepository($class)->matching($criteria)->toArray()
        );

        // Enable the soft-deleted filter again for this entity
        $em->getFilters()->getFilter('softdeleteable')->enableForEntity($class);

        return $result;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.