Symfony2表在Twig中有3个实体

First of all, sorry by advance for my english which is not perfect.

I have a problem for days by reporting 3 doctrine entity in a Twig Table template.

It's a table for manage stocks at work. I have different materials which have each different sizes. Each couple (1 material + 1 size) got a number as amound to order.

So I first created 3 entity : Stock (represent materials) ManyToMany Dimension (represent sizes) Besoin (needs) got a ManyToOne relation with both Stock and Dimension.

Then, I created few Stocks, Dimensions and needs with my forms to get a test database.

The goal is now to create a double entry table with the dimensions list, Stocks list and in each cell the needed number. It's in this step that I have bugs.

For now I can give you my code and hope someone can help me by giving me a tips.

Controller :

public function commandeAction()
    {
        $em = $this->getDoctrine()->getManager();
        $materiauxlist = $em->getRepository('TGComptaBundle:Stock')->findAll();
        $dimensionslist = $em->getRepository('TGComptaBundle:Dimension')->findAll();
        $tab1 = array_merge($materiauxlist, $dimensionslist);
        $besoins = array();

        foreach ($materiauxlist as $stock) {
                foreach ($dimensionslist as $dimension) {
                    $besoin = $em->getRepository('TGComptaBundle:Besoin')->findBesoins($stock, $dimension);
                }
        }

        return $this->render('TGProdBundle:Projet:stocks.html.twig', array(
                'materiauxlist' => $materiauxlist,
                'dimensionslist' => $dimensionslist,
                'besoin' => $besoin));
    }

View :

{% block tgprod_body %}

<div class="well">
    <table class="table table-hover table-bordered">
        <thead>
            <tr>
                <th>#</th>
                    {% for dimension in dimensionslist %}
                        <th>{{ dimension.name }}</th>
                    {% endfor %}
            </tr>
        </thead>
                {% for stock in materiauxlist %}
                    <tr>
                        <td>{{ stock.name }}</td>
                             {% for dimension in dimensionslist %}
                                        {% if besoin %}
                                            <td>{{ besoin.nombre }}</td>
                                        {% else %}
                                            <td>X</td>
                                        {% endif %}
                                {% endfor %} 
                    </tr>
                {% endfor %}
    </table>
</div>

{% endblock %}

Repository :

public function findBesoins($stock, $dimension)
    {
        $qb = $this->createQueryBuilder('b');

        $qb
            ->where('b.stock = :stock')
            ->andwhere('b.dimension = :dimension')
            ->setParameter('stock', $stock)
            ->setParameter('dimension', $dimension);

        $qb
            ->getQuery()
            ->getResult();
    }

My actual problem is : I have X in all cells

Before I asked your help I tried :

foreach ($materiauxlist as $stock) {
                foreach ($dimensionslist as $dimension) {
                    $besoin = $em->getRepository('TGComptaBundle:Besoin')->findBesoins($stock, $dimension);
                    $besoins[] = $besoin;
                }
        }

and :

<table class="table table-hover table-bordered">
        <thead>
            <tr>
                <th>#</th>
                    {% for dimension in dimensionslist %}
                        <th>{{ dimension.name }}</th>
                    {% endfor %}
            </tr>
        </thead>
                {% for stock in materiauxlist %}
                    <tr>
                        <td>{{ stock.name }}</td>
                             {% for dimension in dimensionslist %}
                                        {% for besoin in besoins %}
                                            {% if besoin %}
                                                <td>{{ besoin.nombre }}</td>
                                            {% else %}
                                                <td>X</td>
                                            {% endif %}
                                        {% endfor %}
                                {% endfor %} 
                    </tr>
                {% endfor %}
    </table>

But it was wrong result (I had like 10000 X on each row) I thinked it's the "{% for besoin in besoins %}" which is wrong, it should be something like "{% for besoin.dimension.stock in besoins %}" but I can't write this in twig.

and I tried something like this to :

$table = array('stock' => $stock, 'dimension' => $dimension, 'besoin' => $besoin);
                    $besoins[] = $table;

when I do a { dump() } in Twig I have array with number of stock x number of dimension and all with a null besoin so it seems strange ... and finally I didn't find how to render this array in my table so I let this solution away :/