我试图弄清楚如何根据条目的字段显示many
关系条目。
因此,category
有很多hardware
条目,而hardware
条目有一个字段client
。
我现在想要在我的树枝模板中显示category
如果category
不为空(硬件条目也是如此),并且具有特定客户端(id)的硬件条目的数量是&gt ; 0
所以......这样(这个doe不起作用):
{% if type.hardware.contains(client)|length > 0 %}
{# DO whatever #}
{% endif %}
client
是来自ManyToOne
的{{1}}连接。
有没有办法在Twig中做到这一点,还是我必须在我的php控制器中解决它?
// EDIT
好的,我知道最好将逻辑放在我的控制器中,我想出了这个,但如果没有附加硬件(它应该),这不显示类型
hardware
因此,如果某个类型具有硬件,则会显示,否则不会。我试着添加
$types = $qb->select(array('t','h'))
->from('AppBundle:Type', 't')
->join('t.hardware', 'h')
->where('h.client = :client')
->setParameter('client', $client)
->orderBy('t.sortOrder', 'ASC')
->getQuery()
->getResult();
但是会抛出一个mysql错误:
第0行,col 100靠近'hardware IS NULL':错误:无效的PathExpression。
答案 0 :(得分:1)
这些,它们都来自一个有效的项目,这取决于你如何检索你的数据:
{% if type.hardware.contains(client) %}
// or
{% if type.hardware.contains(client) not empty %}
// or
{% if type.hardware.contains(client)|length != 0 %}