简体   繁体   English

如何从Symfony2实体字段中的Twig模板检索基础实体对象?

[英]How do I retrieve the underlying entity object from a Twig template in a Symfony2 entity field?

I have a form builder which creates a form with an 'entity' field type (let's call that entity MyBundle:MyEntity). 我有一个表单生成器,它创建一个带有“实体”字段类型的表单(让我们称之为实体MyBundle:MyEntity)。 MyEntity has a property entitled 'description'. MyEntity有一个名为'description'的属性。 In addition to the __tostring() method automatically called by Symfony2 that is used for the label of each radio button, I'd like to access the description property. 除了 Symfony2自动调用的__tostring()方法用于每个单选按钮的标签之外,我还想访问description属性。

I've overridden the choice_widget block and attempted to get {{ child.vars.value }} , but that's just the ID of the entity rather than the object itself. 我已经覆盖了choice_widget块并尝试获取{{ child.vars.value }} ,但这只是实体的ID而不是对象本身。

Does anyone know how to do this, or if it is even possible? 有谁知道怎么做,或者甚至可能吗?

I didn't understand well what you want to do exactly since you didn't provide any code but from what I understood you want to access the object of the child right? 我不太清楚你想要做什么,因为你没有提供任何代码,但是根据我的理解你想要访问孩子的对象吧?

If that's the case you can use child.get('value') and this will return the object 如果是这种情况,您可以使用child.get('value'),这将返回该对象

Which means you can call child.get('value').getDescription() or whatever function child object has 这意味着您可以调用child.get('value')。getDescription()或子对象具有的任何函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM