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