Reputation: 2683
I am trying to figure out how to show many
relationship entries based on a field of the entry.
So, a category
has many hardware
entries, and a hardware
entry has a field client
.
I would now like to display the category
in my twig template IF the category
is not empty (so has hardware entries) AND the count of hardware entries WITH a specific client(id) is > 0.
So sth like this (this doe not work):
{% if type.hardware.contains(client)|length > 0 %}
{# DO whatever #}
{% endif %}
client
is a ManyToOne
connection from hardware
.
Is there a way to do that in Twig or do I have to work around it in my php controller?
//EDIT
Ok, I understand it is better to put the logic in my controller, I came up with this, but this does not show the type if there is no hardware attached (it should)
$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();
So if a type has hardware it will show, otherwise it will not. I tried to add
->orWhere($qb->expr()->isNull('t.hardware'))
but that throws a mysql error:
line 0, col 100 near 'hardware IS NULL': Error: Invalid PathExpression.
Upvotes: 0
Views: 3063
Reputation: 16103
How about these, they all come out of a working project, it depends on how you retreive your data:
{% if type.hardware.contains(client) %}
// or
{% if type.hardware.contains(client) not empty %}
// or
{% if type.hardware.contains(client)|length != 0 %}
Upvotes: 2