3

When I tried to display a related object in Twig, and that relation is not present because the ID is in the parent entity, but the related entity was not in the current database, Symfony throws a 500 error

// EntityNotFoundException Twig_Error_Runtime An exception has been thrown during the rendering of a template ("Entity of type 'App\Entity\Location' for IDs id(265) was not found").

I'd like to be able to ignore this error and instead display something like "Related object missing".

It seemed like this could be solved by some checking in Twig, but checking if the relation is null or not defined does not work - they both still find the relation, but when a property is called on the related entity, the exception is thrown.

Has anyone solved this problem already?

Geoff Maddock
  • 1,700
  • 3
  • 26
  • 47

2 Answers2

4

You could check if the entity exists in a Twig extension

Something like:

public function isRelatedEntityDefined($entity)
{    
   try {
        if(isset($entity->getSomeField()) return true;        
   } catch (EntityNotFoundException $e) {
        return false;
   }
}
Jeroen
  • 1,991
  • 2
  • 16
  • 32
  • Good idea. I tried this out and it does work. However, I'd like to implement something more generic that would catch this in every case rather than having to call a twig filter everywhere I might display a relation. – Geoff Maddock Mar 21 '18 at 23:43
  • What exactly are you trying to display ? If you use the toString method of the entity you could implement the try catch there ? – Jeroen Mar 22 '18 at 08:26
  • If I do that, I get an error stating that toString must now throw an exception such as: Error: Method Proxies\__CG__\App\Entity\Location::__toString() must not throw an exception, caught Doctrine\ORM\EntityNotFoundException: Entity of type 'App\Entity\Location' for IDs id(265) was not found – Geoff Maddock Mar 22 '18 at 13:21
  • Is that with a try catch like in the Twig extension ? – Jeroen Mar 22 '18 at 14:47
  • That's if I move the try-catch into the toString. Which would prob be sufficient for me if it worked. – Geoff Maddock Mar 23 '18 at 20:43
  • Well is it possible to try and solve it by making sure the entity is fetched or not in the result at all ? Else i think your best bet is to use a Twig filter / function or pre check the results in a service. – Jeroen Mar 25 '18 at 12:42
0

Have a look at this subject On delete cascade with doctrine2

Isn't the problem from your annotation ?

On your owning side the ID is still defined but the entity doesn't exist anymore.

You should do something like this :

* @JoinColumn(name="locationId", referencedColumnName="id", onDelete="set null")
Gregoire Ducharme
  • 1,095
  • 12
  • 24
  • This is definitely a sane comment. However, I'd like to be able to ignore the integrity constraint here, because in this case I need to be able to account for working with a database that has been partially restored, and inherently will have some of these danging relationships. – Geoff Maddock Mar 22 '18 at 13:19
  • Got it. I see your point. I don't know then, I assume your not supposed to have this kind of situtation as Doctrine should throw an exception of some kind. – Gregoire Ducharme Mar 22 '18 at 13:24