我有一个在doctrine.I许多关系要计算每个相关领域和嫩枝显示它们对循环嫩枝和Doctrine-计数的枝条循环各相关实体和显示
到目前为止
一个VP是关系到Voters.Vp有许多选民和选民有一个副总裁 我想每个VP
计算每个相关的选民public function getAllVp()
{
return $this
->createQueryBuilder('v')
->select('vp.id,COUNT(v.id) as num')
->from('Voters', 'v')
->join('v.Vp', 'vp')
->orderBy('v.id', 'ASC')
->getQuery()
->getResult()
;
}
我想这在枝杈像
{% for vp in vps %}
{{ vp.firstname }}
{{ vp.num }}//number of voters
{% endfor %}
控制器
$vice_president = $em->getRepository('Bundle:Vp')->getAllVp();
return $this->render('Bundle:Vp:all_vp.html.twig', array(
'vps' => $vice_president,
));
学说
fields:
firstname:
type: string
length: 255
lastname:
type: string
length: 255
photo:
type: string
length: 255
oneToMany:
voters:
targetEntity: Voters
mappedBy: vp
我得到这个错误
[Semantical Error] line 0, col 94 near 'vp, Voters v': Error: Class Project\Bundle\DuterteBundle\Entity\Vp has no association named Vp
如何正确教义实现这一目标?
更新
voters.orm.yml
manyToOne:
vp:
targetEntity: Vp
cascade: { }
mappedBy: null
inversedBy: voters
joinColumn:
name: vp_id
referencedColumnName: id
orphanRemoval: false
我可以通过简单地调用实现了这个相关的“选民”,并添加一个过滤器Twig.But我的本意是统计数据在教义,重用其他模板或将其转换为JSON的未来,例如在角JS
{% if vp.voters|length > 0 %}
<tr {% if loop.index is odd %}class="color"{% endif %}>
<td>{{ vp.id }}</td>
<td>{{ vp.getFullName() }}</td>
<td>{{ vp.voters|length|number_format }}</td>
</tr>
{% endif %}
上面是一个工作代码BU T I想做的计数原则,而不是在模板
预期结果
id fullname counts
1 George Bush 45
2 ali gail 1999
4 Mae Young 45
......
请问,你可以发布选民实体的学说映射吗? – Delphine
@ Delphine我会更新我的问题。 –