Exception handling inside a branch template

I am new to symfony2. my project has two objects

    [1] Category and
    [2] Evaluation

and the category has many ratings, so the problem is that I delete the category and then show the rating, then it shows me an error, for example

"An exception has been thrown during the rendering of a template ("Entity was not found.") in HfAppBundle:SpecificEvaluations:index.html.twig at line 137. ".

line 137 is the content {{evaluation.category.name}}. I also tried

    {% if evaluation.category.name is not null %}
        {{evaluation.category.name}}
    {% endif %}

but also gives me the same error. can anyone help?

thanks

+4
source share
3 answers

Instead of checking the category name, check if there is a category associated with the rating.

{% if evaluation.getCategory  %}
        {{evaluation.category.name}}
{% endif %}

Ideally, when you delete a category associated with multiple ratings, you should remove the link between the deleted category and ratings.

, null . yml

manyToOne:
        user:
          targetEntity: LB\CoreBundle\Entity\User
          joinColumn:
            name: user_id
            referencedColumnName: id
            onDelete: "SET NULL"

OnDelete "SET NULL", "CASCADE" , null , .

, . , .

$evaluations = $em->getRepository('HfAppBundle:Evaluation')->findByCategory($catId); 

foreach ($evaluations as $evl) { 
$evl->setCategory(null);
//this line was missing
 $em->persist($evl);
} 

$em->flush(); 
+1

defined:

{% if evaluation.category.name is defined %}
    {{evaluation.category.name}}
{% endif %}
+1

default:

{{ evaluation.category.name|default('[No name]') }}
+1

Source: https://habr.com/ru/post/1606201/


All Articles