【问题标题】:Symfony2, How to cast data type in a twig file?Symfony2,如何在树枝文件中转换数据类型?
【发布时间】:2012-05-24 20:08:59
【问题描述】:

我有一些类似下面的场景:

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

【问题讨论】:

    标签: symfony twig


    【解决方案1】:

    rolesFeatures 是一个数组,因此您需要对其进行迭代。比如:

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

    【讨论】:

    • 我得到这个错误:Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::serialize() 必须返回一个字符串或 NULL
    • @PMoubed 是否适合您?因为我有类似的问题here。如果可以的话,谢谢你的帮助。
    猜你喜欢
    • 2015-04-22
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 2015-12-10
    • 2012-09-16
    相关资源
    最近更新 更多