6

i am new to symfony2. my project has two entity

    [1] Category and
    [2] Evaluation

and category has many evaluation, so the problem is when i delete the category and then display the evaluation then it display me error like

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

on line number 137 this is the content {{evaluation.category.name}}. i had also try with

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

but it also give me same error. any one can help ?

thanks

Dr Magneto
  • 981
  • 1
  • 8
  • 18
  • if difficult to catch an exception raised from doctrine when an entity is not found only when you call the getter method. Try to change the twig environment `variables debug=false` and `strict_variables=false` in the `config.yml` check the doc for further detail [here](http://twig.sensiolabs.org/doc/api.html#environment-options) – Matteo Sep 07 '15 at 07:24

3 Answers3

1

Use twig test defined :

{% if evaluation.category.name is defined %}
    {{evaluation.category.name}}
{% endif %}
malcolm
  • 5,486
  • 26
  • 45
0

Instead of checking for the category name, check whether the category associated with an evaluation exists.

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

Ideally, when you delete a category that is linked to multiple evaluations, you should remove the relation between the deleted category and the evaluations.

For this, specify whether to delete all the evaluations when a category is deleted or to set the category of all related evaluations to null when deleting the category. For this, in yml the relation should be defined as

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

The onDelete can be either "SET NULL" or "CASCADE" depending on whether you need set the category field on evaluation as null or delete all the evaluations that are related to a category.

Modify your code for setting the category on evaluations as given below. You were not persisting the evaluations after setting the categorry to null.

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

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

$em->flush(); 
Praveesh
  • 1,257
  • 1
  • 10
  • 23
  • i had also try with this solution but it won't work. it's give me same error message. – Dr Magneto Sep 07 '15 at 06:23
  • I have modified the answer. Could you please give it a try? – Praveesh Sep 07 '15 at 06:25
  • when i delete the category i was manualy set category_id field of evaluation to null like. $evaluations = $em->getRepository('HfAppBundle:Evaluation')->findByCategory($catId); foreach ($evaluations as $evl) { $evl->setCategory(null); $em->flush(); } but still when i fetch the evaluation then i get true in {% if evaluation.category.name is defined %} – Dr Magneto Sep 07 '15 at 07:03
  • I think you are missing ````$em->persist($evl);```` inside the for loop. – Praveesh Sep 07 '15 at 07:09
  • i don't know but it set to 0 in DB, (because i havn't allowd null) – Dr Magneto Sep 07 '15 at 07:11
0

Give a try to the default filter:

{{ evaluation.category.name|default('[No name]') }}
Mario
  • 8,253
  • 2
  • 14
  • 29
Yassine Guedidi
  • 1,695
  • 11
  • 12