2012-05-24 73 views
0

我有一些場景象下面這樣:Symfony2,如何在樹枝文件中投射數據類型?

/** 
* @ORM\Entity 
* @ORM\Table(name="role") 
*/ 
class Role 
{ 
    /** 
    * @ORM\OneToMany(targetEntity="RolesFeatures", mappedBy="role", cascade={"all"}) 
    **/ 
    private $rolesFeatures; 
} 

在我的索引文件,我想,讓他們:

{{ role.rolesFeatures.getId() }} 

我得到這個:

An exception has been thrown during the rendering of a template ("Catchable Fatal 
Error: Object of class Doctrine\ORM\PersistentCollection could not be converted to string 
in C:\wamp\www\PMI_sf2\app\cache\dev\twig\63\81\679fca1c2da64d0ebbcd5661bc6d.php line 99") 
in PMIHomePagesBundle:HomePages:mainHome.html.twig at line 49. 

我如何可以投學說\ ORM \ PersistentCollection它真正的對象類?

回答

4

rolesFeatures是一個數組,因此您需要遍歷它。喜歡的東西:

{% for roleFeature in role.rolesFeatures %} 
    {{ roleFeature.id }} 
{% endfor %} 
+0

我得到這個錯誤:Symfony的\分量\安全\核心\認證\令牌\ UsernamePasswordToken ::連載()必須返回一個字符串或NULL – PMoubed

+0

我有一些私人的會員,所以我得到了錯誤信息。 – PMoubed

+0

@PMoubed它適合你嗎?因爲我有一個類似的問題[這裏](http://stackoverflow.com/questions/28175708/query-a-manytomany-relation-and-display-the-good-result-in-symfony-with-doctrine)。如果可以的話,謝謝你的幫助。 – 2015-01-28 15:57:54