简体   繁体   中英

Render multi-array in TWIG

I post next to my problem here

Because I can't render my array in Twig.

A {{ dump(besoins) }} give me something like

 array (size=30)   0 => 
     array (size=3)
       'stock' => 
         object(TG\ComptaBundle\Entity\Stock)[1097]
           private 'dimensions' => 
             object(Doctrine\ORM\PersistentCollection)[1131]
               ...
           private 'id' => int 1
           private 'name' => string 'Dilite 2' (length=8)
           private 'prix' => int 15
       'dimension' => 
         object(TG\ComptaBundle\Entity\Dimension)[1134]
           private 'stocks' => 
             object(Doctrine\ORM\PersistentCollection)[1123]
               ...
           private 'id' => int 10
           private 'name' => string '15 x 15' (length=7)
           private 'longueur' => int 15
           private 'largeur' => int 15
       'besoin' => 
         array (size=1)
           0 => 
             object(TG\ComptaBundle\Entity\Besoin)[1773]
               ...

So I can see that I have "besoin" in my array.

But with my twig code, my cells stay empty ... :(

<table class="table table-hover table-bordered">
        <thead>
            <tr>
                <th>#</th>
                    {% for dimension in dimensionslist %}
                        <th>{{ dimension.name }}</th>
                    {% endfor %}
            </tr>
        </thead>
        <tbody>
                {% for stock in materiauxlist %}
                    <tr>
                        <td>{{ stock.name }}</td>
                        {% set newArray = [] %}
                        {% for tableau in besoins %}
                            {% if tableau.stock.name == stock.name %}
                                {% set newArray = newArray|merge([tableau]) %}
                            {% endif %}
                        {% endfor %}
                        {% for tableau in newArray %}
                                {% if besoin %}
                                    <td>{{ besoin.nombre }}</td>
                                {% endif %}
                        {% endfor %}
                    </tr>
                {% endfor %}
    </tbody>
    </table>

Here is my 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')->findBy(array('stock' => $stock, 'dimension' => $dimension), null, 1);
                    $tableau = array('stock' => $stock, 'dimension' => $dimension, 'besoin' => $besoin);
                    $besoins[] = $tableau;
                }
        }

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

Please can someone help me ?

I think this is wrong:

{% for tableau in newArray %}
   {% if besoin %}
      <td>{{ besoin.nombre }}</td>
   {% endif %}
{% endfor %}

Edit: it should look like:

{% for values in newArray %}
   {% for key,value in values %}
      {% if key == "besoin" %}
         {% for bison in value %}
            <td>{{ bison.nombre }}</td>
         {% endfor %}
      {% endif %}
   {% endfor %}
{% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM