2014-08-30 53 views
-2

當試圖輸出與任務關聯的所有附件(附件實體)時,我會得到以下異常。但我不知道爲什麼。未定義指數:symfony2從OneToMany實體輸出數據TWIG

一個例外模板(簡稱「通知的渲染過程中被拋出任務/應用程序/ MAMP/htdocs中/ Seotool /供應商/教義/ ORM/lib中/教義/ ORM /持久化/ BasicEntityPersister在/Applications/MAMP/htdocs/Seotool/src/Seotool/MainBundle/Resources/views/Task/load_task.html.twig .PHP線1753" )在管線10

TWIG:

{{ tasks.taskDescription }} 
{% for attachment in tasks.Attachments %} 
    {{ attachment.id }} 
{% endfor %} 

控制器:

/** 
@Route(
*  path = "/tasks/load/{id}", 
*  name = "load_task" 
*) 
* @Template() 
*/ 
public function load_taskAction($id, Request $request) 
{ 

    $tasks = $this->getDoctrine() 
     ->getRepository('SeotoolMainBundle:Task') 
     ->find($id); 

    return array(
     'tasks' => $tasks 
    ); 

} 

任務實體:

/** 
* @ORM\OneToMany(targetEntity="Attachments", mappedBy="task",cascade={"persist"}) 
*/ 
protected $attachments; 

附件實體:

/** 
* @ORM\ManyToOne(targetEntity="Task", inversedBy="attachments") 
* @ORM\JoinColumn(name="task", referencedColumnName="id") 
*/ 
protected $Task; 
+1

更改保護$任務;保護$任務; 您可能會收到另一條錯誤: {%爲附件在任務中。附件%}將其更改爲 {%for attachment in tasks.attachments%} – mohsenJsh 2014-08-30 16:55:03

+0

謝謝,現在可以使用:) – Marvin 2014-08-30 18:02:21

+0

歡迎您:) 。在更改$ Task之前,它是否正常工作?在您的addAttachment()方法中放入$ attachment-> addTask($ this); *** ; – mohsenJsh 2014-08-30 19:19:13

回答

1
mappedBy="task" 

大概應該是

mappedBy="Task" 

或更好的工作性質必須以小寫字母開頭,這是更標準的方法。在這種情況下,您應該更改此參考:

targetEntity="Task"