Reputation: 2824
I am having a heck of a time trying to escape twig errors when an entity is not found.
I have a table called listings and it has a relationship to warehouses. If a warehouse is deleted, then I am getting this error in Twig "An exception has been thrown during the rendering of a template ("Entity was not found.")".
I have tried all of these options to suppress this error, but I cannot seem to accomplish anything. Please help if you can. Thanks!
{% if inventoryLocation.Warehouse.name is defined %}
{{ inventoryLocation.Warehouse.name }}
{% endif %}
Created this error: "An exception has been thrown during the rendering of a template ("Entity was not found.")"
{% if inventoryLocation.Warehouse %}
{{ inventoryLocation.Warehouse.name }}
{% endif %}
Created this error: "An exception has been thrown during the rendering of a template ("Entity was not found.")"
{% if inventoryLocation.Warehouse is not null %}
{{ inventoryLocation.Warehouse.name }}
{% endif %}
Created this error: "An exception has been thrown during the rendering of a template ("Entity was not found.")"
{% if inventoryLocation.Warehouse sucks because its not there %}
{{ inventoryLocation.Warehouse.name }}
{% endif %}
Doesn't really do anything it just makes me really mad :)
Upvotes: 0
Views: 3315
Reputation: 501
I have solved this type of problem by creating a custom getter within my entity or entities that are being iterated on.
For instance - in your case - you have an InventoryLocation and a Warehouse. It looks like you are displaying or iterating on the InventoryLocation entity.
In that case - I would create a custom getter within the InventoryLocation entity that looks something like this:
<?php
public function getWarehouseName()
{
// Get the associated Warehouse
if ($this->getWarehouse())
{
// Return Warehouse name
return $this->getWarehouse()->getName();
}
else
{
return null;
}
}
Then - in your Twig template - you can call this customer getter method and add in a default if the getter returns NULL.
{{ inventoryLocation.warehouseName | default('-') }}
Upvotes: 1