Php 细枝关联对象数组渲染
我的控制器的输出如下所示:Php 细枝关联对象数组渲染,php,symfony,twig,Php,Symfony,Twig,我的控制器的输出如下所示: D:\web\www2\application\src\CommonBundle\Controller\ClassesController.php:22: array (size=2) 'Grade 8' => array (size=1) 0 => array (size=2) 0 => object(CommonBundle\Entity\GradeLevel
D:\web\www2\application\src\CommonBundle\Controller\ClassesController.php:22:
array (size=2)
'Grade 8' =>
array (size=1)
0 =>
array (size=2)
0 =>
object(CommonBundle\Entity\GradeLevel)[363]
...
1 =>
object(CommonBundle\Entity\GradeLevel)[367]
...
'Grade 9' =>
array (size=1)
0 =>
array (size=1)
0 =>
object(CommonBundle\Entity\GradeLevel)[372]
...
这些数据来自一个One-T-\u-Many关系,我想做的是在我的html表上进行分组。下面是我创建对象数组的方式:
$grades = $this->getDoctrine()->getRepository('CommonBundle:Grade')->findAll();
$classes = array();
foreach($grades as $grade){
$classes[$grade->getName()][] = $this->getDoctrine()->getRepository('CommonBundle:GradeLevel')->findBy(array(
'grade' => $grade->getId()
));
}
return $this->render('CommonBundle:Classes:index.html.twig', array(
'classes' => $classes
));
如果这是一段普通的PHP代码,我会简单地执行以下操作:
<?php
foreach($classes as $key => $values){
...
<td colspan="4"><?php echo $key //Being the category name as indexed from the controller ?></td>
//And continue with the contents of the classes
foreach($values as $class){
<tr>
<td><?php echo $class->getName(); ?>
....
}
}
//然后继续学习课程内容
foreach($类的值){
....
}
}
我发现很难在网上找到满足我需求的解决方案。有谁能给我指出一个正确的方向,因为TWIG文档在这方面似乎也是抽象的
编辑:
下面是当前的表格结构,我必须使任何助手都能轻松了解我要实现的目标
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Class</th>
<th>Created on</th>
<th>Modified at</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for values in classes %}
<tr>
<!--- Category name should go here e.g "Grade 8" then next loop should list all Grades from that category -->
<td colspan="5" align="right">{{ values }}</td>
</tr>
{% for item in values %}
<tr>
<td> </td>
<td>{{ item.name }}</td>
<td>{{ item.created|date('Y-m-d H:i:s') }}</td>
<td>{{ item.modified|date('Y-m-d H:i:s') }}</td>
<td><a class="btn btn-sm btn-primary" href="#" role="button">Summary</a></td>
</tr>
{% endfor %}
{% endfor %}
</tbody>
#
等级
创建于
修改于
行动
{%类中的值为%}
{{values}}
{值%%中的项的百分比}
{{item.name}
{{item.created}日期('Y-m-dh:i:s')}
{{item.modified}日期('Y-m-dh:i:s')}
{%endfor%}
{%endfor%}
编辑2
您需要在细枝中执行与在php-loop-inside-loop中相同的操作 请记住,您的阵列中有3个级别
{% for values in classes %}
{% for item in values[0] %}
<li>{{ item.name }}</li>
{% endfor %}
{% endfor %}
{%类中的值%}
{%用于值[0]%中的项
{{item.name}
{%endfor%}
{%endfor%}
使用值[0]
访问等级*
下的第一个(也是唯一的元素)
从性能的角度来看,这不是最好的选择,但是如果您不能更改数据结构,那么您可以使用它。这是我在视图中的最后一个表代码,它可以正确地按需要呈现
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Class</th>
<th>Created on</th>
<th>Modified at</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for group, values in classes %}
<tr>
<td colspan="5" align="left"><strong>{{ group }}</strong></td>
</tr>{% for item in values %}
<tr>
<td> </td>
<td>{{ item.name }}</td>
<td>{{ item.created|date('Y-m-d H:i:s') }}</td>
<td>{{ item.modified|date('Y-m-d H:i:s') }}</td>
<td><a class="btn btn-sm btn-primary" href="#" role="button">Summary</a></td>
</tr>
{% endfor %}
{% endfor %}
</tbody>
#
等级
创建于
修改于
行动
{对于组,值在类%}
{{group}}
{值%%中的项的百分比}
{{item.name}
{{item.created}日期('Y-m-dh:i:s')}
{{item.modified}日期('Y-m-dh:i:s')}
{%endfor%}
{%endfor%}
谢谢@Pawel。我添加了我的HTML,这样你就可以真正看到我在做什么。我希望这会有所帮助,因为我仍然无法访问数组对象的键字符串。在您的更新中,对于实现上面我的更新中的图片所示的性能,您的最佳建议是什么?提前谢谢。