Symfony2,如何在树枝文件中转换数据类型? [英] Symfony2, How to cast data type in a twig file?

查看:29
本文介绍了Symfony2,如何在树枝文件中转换数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些像下面这样的场景:

I have some scenario like below :

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

在我的索引文件中,我想得到它们:

In my index file I would like to get them:

{{ 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 转换为真实的对象类?

How I can Cast Doctrine\ORM\PersistentCollection to it real object class ?

推荐答案

rolesFeatures 是一个数组,因此您需要对其进行迭代.类似的东西:

rolesFeatures is an array so you need to iterate over it. Something like:

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

这篇关于Symfony2,如何在树枝文件中转换数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆