我在我的問題旁發貼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));
}
請有人可以幫助我
你肯定$ besoin進行評估,以真實的樹枝'{%如果besoin%}'? – KmeCnin
沒有那麼重要,它被評估爲錯誤我認爲但我不知道爲什麼導致我可以看到我的轉儲中的值 – Nico
不,你說你傾倒besoinS,所以這與besoin沒有關係(沒有S) – KmeCnin