Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 / Doctrine: Reading "deleted" data when using Gedmo's doctrine extensions

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.

like image 635
Nathan Rutman Avatar asked Oct 23 '13 18:10

Nathan Rutman


3 Answers

You can do this by :

$filter = $em->getFilters()->enable('soft-deleteable');
$filter->disableForEntity('Entity\User');
$filter->enableForEntity('Entity\Note');
like image 106
trrrrrrm Avatar answered Nov 20 '22 10:11

trrrrrrm


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;
    }
}
like image 44
Oli Avatar answered Nov 20 '22 11:11

Oli


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.

like image 1
Nada_Surf Avatar answered Nov 20 '22 09:11

Nada_Surf