问题描述
首先,对于我的英语提前不好,这不是完美的。
我在一个Twig Table模板中报告3个学说实体,有几天的问题。
这是用于管理工作中的股票的表格。我有不同的材料,每个不同的大小。每对夫妇(1材料+ 1大小)得到一个数字令人难以置信。
所以我首先创建了3个实体:
股票(代表材料)ManyToMany维度(代表大小)
Besoin(需求)得到了一个ManyToOne关系与库存和维度。
然后,我创建了几个库存,维度和需求与我的表单获取测试数据库。
目标是创建一个带有维度列表,库存列表和每个单元格所需数量的双重条目表。
在这一步中我有错误。
现在我可以给你我的代码,希望有人可以帮我一个提示。 p>
控制器:
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>
{维度中的维度列表%}
< th> {{dimension.name}}< / th>
{%endfor%}
< / tr>
< / thead>
{%for materiauxlist%}
< tr>
< td> {{stock.name}}< / td>
{维度中的维度列表%}
{%如果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
- >其中('b.stock =:stock')
- >和where('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>
{维度中的维度列表%}
< th> {{dimension.name}}< / th>
{%endfor%}
< / tr>
< / thead>
{%for materiauxlist%}
< tr>
< td> {{stock.name}}< / td>
{%维度中的维度列表%}
{%for besoin在besoins%}
{%如果besoin%}
< td> {{besoin.nombre}}< ; / TD>
{%else%}
< td> X< / td>
{%endif%}
{%endfor%}
{%endfor%}
< / tr>
{%endfor%}
< / table>
但是这是错误的结果(我在每一行都有10000 X )
我认为这是{bescoin in besoins%},这是错误的,它应该是像{%for besoin.dimension.stock在besoins%},但我不能写这个
,我尝试过这样的一个例子:
$ table = array('stock'=> $ stock,'dimension'=> $ dimension,'besoin'=> $ besoin);
$ besoins [] = $ table;
当我在Twig中执行{dump()}我有数组,维度和所有与一个null besoin,所以它看起来很奇怪...最后我没有找到如何在我的表中渲染这个数组,所以我让这个解决方案:/
First of all, sorry by advance for my english which is not perfect.
I have a problem for days by reporting 3 doctrine entity in a Twig Table template.
It's a table for manage stocks at work. I have different materials which have each different sizes. Each couple (1 material + 1 size) got a number as amound to order.
So I first created 3 entity :Stock (represent materials) ManyToMany Dimension (represent sizes)Besoin (needs) got a ManyToOne relation with both Stock and Dimension.
Then, I created few Stocks, Dimensions and needs with my forms to get a test database.
The goal is now to create a double entry table with the dimensions list, Stocks list and in each cell the needed number.It's in this step that I have bugs.
For now I can give you my code and hope someone can help me by giving me a tips.
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')->findBesoins($stock, $dimension);
}
}
return $this->render('TGProdBundle:Projet:stocks.html.twig', array(
'materiauxlist' => $materiauxlist,
'dimensionslist' => $dimensionslist,
'besoin' => $besoin));
}
View :
{% 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 %}
Repository :
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();
}
My actual problem is : I have X in all cells
Before I asked your help I tried :
foreach ($materiauxlist as $stock) {
foreach ($dimensionslist as $dimension) {
$besoin = $em->getRepository('TGComptaBundle:Besoin')->findBesoins($stock, $dimension);
$besoins[] = $besoin;
}
}
and :
<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>
But it was wrong result (I had like 10000 X on each row)I thinked it's the "{% for besoin in besoins %}" which is wrong, it should be something like "{% for besoin.dimension.stock in besoins %}" but I can't write this in twig.
and I tried something like this to :
$table = array('stock' => $stock, 'dimension' => $dimension, 'besoin' => $besoin);
$besoins[] = $table;
when I do a { dump() } in Twig I have array with number of stock x number of dimension and all with a null besoin so it seems strange ... and finally I didn't find how to render this array in my table so I let this solution away :/
这篇关于Symfony2表中有3个实体在Twig的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!