2015-07-21 73 views
0

我在我的问题旁发贴here在TWIG中渲染多个阵列

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

A {{转储(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)); 
    } 

请有人可以帮助我

+0

你肯定$ besoin进行评估,以真实的树枝'{%如果besoin%}'? – KmeCnin

+0

没有那么重要,它被评估为错误我认为但我不知道为什么导致我可以看到我的转储中的值 – Nico

+2

不,你说你倾倒besoinS,所以这与besoin没有关系(没有S) – KmeCnin

回答

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 %} 
+0

Thx但悲伤地“键”nombre“阵列与键”股票,维度,besoin“不存在TGProdBundle:Projet:stocks.html.twig第31行”。如果我把{{value.besoin}},而不是我有一个错误arraytoString转换:/ – Nico

+0

'{%if tableau.besoin | first%}' – KmeCnin

+0

这是因为比斯是一个集合 – Nickolaus