2016-05-22 17 views
3

異常被拋出的第46行一個例外模板的呈現模板</p> <blockquote> <p>Catchable Fatal Error: Object of class DoctrineORMPersistentCollection could not be converted to string</p> </blockquote> 在<code>AppBundle:Dashboard:index.html.twig</code><p>的渲染過程中

這是因爲appointment.employees發生時被拋出。有人可以解釋嗎? 這與關係的實體:

/** 
* @ORM\ManyToMany(targetEntity="LamecoEmployee") 
* @ORM\JoinTable(name="appointment_lamecoemployee", 
*  joinColumns={@ORM\JoinColumn(name="appointment_id", referencedColumnName="id")}, 
*  inverseJoinColumns={@ORM\JoinColumn(name="lamecoempoloyee_id", referencedColumnName="id")} 
*  ) 
*/ 
protected $employees; 

這是視圖:

{% for appointment in appointments %} 
    <tr> 
     <td>{{appointment.startDate|date('H:i')}}</td> 
     <td>{{appointment.client.companyname}}</td> 
     <td></td> 
     <td>{{appointment.employees}}</td> 
     <td>{{appointment.description}}</td> 
    </tr> <br> 
{% endfor %} 

回答

5

你必須通過appointment.employees迭代,因爲它是一個集合(同約會),這或將其轉換爲一些樹枝可以使在線。

E.g.

{% for employee in appointment.employees %} 
    {{employee.name}} 
{% endfor %} 

或者,如果你只是想一個數,沒有迭代:

{{appointment.employees|length}} 
0

好吧,你想打印員工的集合。 PHP根本不知道該怎麼做。

您的屬性$employees上的註釋@ORM\ManyToMany表示約會可以鏈接到多個LamecoEmployee(並且員工可以鏈接到多個約會)。因此,$ employee屬性的類型不是LamecoEmployee,它是LamecoEmployee的集合。

如果您在LamecoEmployee類中定義__toString()方法,但無法一次打印一組員工,則可以打印一名員工。要打印預約的所有員工,您可以循環在appointment.employees

{% for employee in appointment.employees %} 
    {{ employee }} 
{% endfor %} 
相關問題