Symfony2,如何在twig文件中转换数据类型?

时间:2012-05-24 20:08:59

标签: symfony twig

我有一些情况如下:

/**
 * @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.

如何将Doctrine \ ORM \ PersistentCollection转换为真实对象类?

1 个答案:

答案 0 :(得分:4)

rolesFeatures是一个数组,因此您需要迭代它。类似的东西:

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