Question

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.

Was it helpful?

Solution 2

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;
    }
}

OTHER TIPS

You can do this by :

$filter = $em->getFilters()->enable('soft-deleteable');
$filter->disableForEntity('Entity\User');
$filter->enableForEntity('Entity\Note');

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top