在TWIG中渲染多数组

时间:2015-07-21 14:27:26

标签: php symfony twig

我在问题here

旁边发帖

因为我无法在Twig中渲染我的数组。

A {{dump(besoins)}}给我一些类似

的东西
 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]
               ...

所以我可以看到我有" besoin"在我的阵列中。

但是用我的树枝代码,我的细胞仍然空着...... :(

<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>

这是我的控制器:

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));
    }

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我认为这是错误的:

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

修改:它应该如下所示:

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