0

以下のようなシナリオがあります。

/**
 * @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 を実際のオブジェクト クラスにキャストするにはどうすればよいですか?

4

1 に答える 1

4

rolesFeaturesは配列であるため、反復処理する必要があります。何かのようなもの:

{% for roleFeature in role.rolesFeatures %}
    {{ roleFeature.id }}
{% endfor %}
于 2012-05-24T20:13:25.143 に答える