2014-09-25 52 views
0

我在数据库和一个实体的两个属性:获取的对象与变量

title_one 
title_two 

而且在模板(小枝)我想只有一次来自这些领域。

{% set type = 'two' %} 

我可以:

<div id="title"> 
{% if type == 'one' %} 
    {% entity.title_one %} 
{% elseif type == 'two' %} 
    {% entity.title_two %} 
{% endif %} 
</div> 

它运作良好,但我喜欢做这样的事情:

<div id="title"> 
    {{ entity.title_{{ type }} }} 
</div> 

我如何能做到吗?

+0

你的问题有点不清楚:是否'title_one'和'title_two'独立的实体,或者是他们相同的实体的属性? – lxg 2014-09-25 06:17:28

+0

对不起,他们是同一个实体的属性。 – marksoofig 2014-09-25 06:25:38

回答

0

只需在实体中添加一个辅助方法,如getMyType()。这将返回正确的类型。在树枝模板,你可以访问此方法:

{% set type == myEntity.getMyType() %} 

请注意,您必须将实体注入到在你的控制器模板:

$Response = $this->render(
    "MySomethingBundle:Area:page.html.twig", 
    array('myEntity' => $entityInstance)); 

顺便说一句,你可能希望有看看different strategies of inheritance mapping