Preciel
Preciel

Reputation: 2827

Filter related entity in twig

[SETTINGS]

[DATABASE CONTENT]

container

------------------
| id |    name   |
------------------
|  1 | container |
------------------

box

--------------------------------------
| id | container_id | parent | name  |
--------------------------------------
|  1 |            1 |   null | box 1 |
--------------------------------------
|  2 |            1 |   null | box 2 |
--------------------------------------
|  3 |            1 |      1 | box 3 |
--------------------------------------

[FILES]

inventory.html.twig

{% if container.box|length > 0 %}
    {% for box in container.box %}
        <li class="nav_menu_item">
            <a class="nav_menu_link" href="">{{ box.name }}</a>
            {% if box.inbox|length > 0 %}
                <div class="nav_menu_dropdown">
                    <ul>
                        {% for inbox in box.inbox %}
                            <li class="nav_menu_dropdown_item">
                                <a class="nav_menu_dropdown_link" href="">{{ inbox.name }}</a>
                            </li>
                        {% endfor %}
                    </ul>
                </div>
            {% endif %}
        </li>
    {% endfor %}
{% endif %}

[PROBLEM]

In twig, I would like to filter the related entity list based on it's parent parameter.
Currently, doing {{ container.box|length }} will return 3 box (same problem with {% for ... %})

How can I get all container.box where box.parent is null ?

Upvotes: 0

Views: 442

Answers (1)

hous
hous

Reputation: 2679

try this

{% for box in container.box if box.parent is null %}

Upvotes: 1

Related Questions