[英]Symfony2 Table with 3 entity in Twig
首先,對不起我的英語水平,我很抱歉。
通過在Twig Table模板中報告3個學說實體,我有幾天的問題了。
這是用於管理工作庫存的表。 我有不同的材料,每種材料都有不同的尺寸。 每對夫婦(1種材料+ 1種尺寸)得到一個按順序排列的數字。
因此,我首先創建了3個實體:庫存(表示材料)ManyToMany Dimension(表示尺寸)Besoin(需要)獲得了與Stock和Dimension的ManyToOne關系。
然后,我用表格創建了一些庫存,尺寸和需求以獲取測試數據庫。
現在的目標是創建一個帶有維度清單,庫存清單以及每個單元格中所需編號的雙重條目表。 在此步驟中,我遇到了錯誤。
現在,我可以給您我的代碼,並希望有人可以通過給我一些提示來幫助我。
控制器:
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));
}
查看:
{% 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 %}
倉庫:
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();
}
我的實際問題是:我所有單元格中都有X
在我尋求您的幫助之前,我嘗試過:
foreach ($materiauxlist as $stock) {
foreach ($dimensionslist as $dimension) {
$besoin = $em->getRepository('TGComptaBundle:Besoin')->findBesoins($stock, $dimension);
$besoins[] = $besoin;
}
}
和:
<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>
但這是錯誤的結果(每行有10000 X ),我認為這是“ {%for besoin in besoins%}”的意思,它應該類似於“ {%for besoin.dimension.stock in besoins% }”,但我不能用樹枝寫這個。
我嘗試過這樣的事情:
$table = array('stock' => $stock, 'dimension' => $dimension, 'besoin' => $besoin);
$besoins[] = $table;
當我在Twig中執行{dump()}時,我有一個數組,其中包含庫存數量x維數,並且所有參數都為空besoin,所以這似乎很奇怪...最后,我沒有找到如何在表中呈現此數組的方法所以我放棄了這個解決方案:/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.