php - Symfony 3.4:在树枝中渲染多个数组
问题描述
我得到了一个带有 CollectionType 的表单,它在 SQLite 数据库中存储了一个数组,这是控制器中返回的实体对象的结构:
Expertations {#666 ▼
-id: 9
-date: DateTime @1536749818 {#653 ▶}
-client: 1
-status: 0
-price: 0.0
-expiration: DateTime @1536749818 {#650 ▶}
-tipo: 1
-kw: 12
-piani_casa: 2
-riscaldamento: "1"
-opere_murarie: false
-trifase: false
-sconto: 10.0
-level: 1
-square_meters: 140
-floor: array:6 [▼
0 => 1
1 => 1
2 => 2
3 => 2
4 => 2
5 => 2
]
-ambient: array:6 [▼
0 => Rooms {#671 ▼
-id: 10
-name: "Cucina"
-level: 1
-sq_meter_from: 0.0
-sq_meter_to: 999.0
-punti_prese: 5
-punti_luce: 1
-prese_tv: 1
}
1 => Rooms {#649 ▶}
2 => Rooms {#670 ▶}
3 => Rooms {#669 ▶}
4 => Rooms {#649 ▶}
5 => Rooms {#668 ▶}
]
-name: array:6 [▼
0 => "Cucina"
1 => "Soggiorno"
2 => "Corridoio"
3 => "Camera Padronale"
4 => "Camera Figlia"
5 => "Bagno"
]
-pp: array:6 [▼
0 => 5
1 => 4
2 => 2
3 => 4
4 => 4
5 => 2
]
-pl: array:6 [▼
0 => 1
1 => 1
2 => 2
3 => 1
4 => 1
5 => 2
]
-pt: array:6 [▼
0 => 1
1 => 1
2 => 0
3 => 1
4 => 1
5 => 0
]
-num_circuiti: 5
-num_prese_telefono_dati: 3
-illum_sicurezza: 2
-spd: 1
-imp_ausiliari: 1
}
这应该在 twig 模板中呈现为表格,大部分数据是使用简单的数组访问来检索的,例如{{ item.string }}
. 命名的字段floor, ambient, name, pp, pl, pt
应该在列中呈现,为 item 一行(您应该看到在这个示例中所有元素都包含 5 个键)。
我尝试像往常一样访问该数组,但在尝试访问实际上是整数的键时出现错误(不应该创建?)
这是不起作用的树枝 for 循环:
{% for items in item %}
<tr>
<td>{{ items.floor }}</td>
<td>{{ items.ambient }}</td>
<td>{{ items.name }}</td>
<td>{{ items.pp }}</td>
<td>{{ items.pl }}</td>
<td>{{ items.pt }}</td>
</tr>
{% endfor %}
返回的错误是:Impossible to access an attribute ("floor") on a integer variable ("1").
预期行为:
这些项目应呈现在表格中,例如:第一行,显示floor.0
值,ambient.0.name, name.0
值,pp.0
值,值,pl.0
值pt.0
`,第二列等。
有人会得到正确渲染它的解决方案吗?
解决方案
对于单个 Expertations 对象(名为 yourObject),循环应如下所示:
{% for key in yourObject.floor|keys %}
<tr>
<td>{{ yourObject.floor[key] }}</td>
<td>{{ yourObject.ambient[key] }}</td>
<td>{{ yourObject.name[key] }}</td>
<td>{{ yourObject.pp[key] }}</td>
<td>{{ yourObject.pl[key] }}</td>
<td>{{ yourObject.pt[key] }}</td>
</tr>
{% endfor %}
推荐阅读
- dataframe - 在 Julia 中查找数据框行 ID
- java - ValidationException:只能在使用 DynamoDB Put 使用表达式时指定 ExpressionAttributeNames
- payment-gateway - Braintree 创建的订阅未显示在 Sandox 帐户中
- r - 用缺失值填充的数据框 - complete() 函数
- android - 从 JSON 到 Kotlin 的图像
- greasemonkey - 无法在 Frontend Mentor 上使用 Tampermonkey 添加输入元素
- linux - 在另一个应用程序正在使用它时嗅探串行端口 /dev/ttyS0
- performance - 稀疏矩阵加速八度
- stata - 如何计算Stata中的浮动置信区间?
- reactjs - 一旦拖动轨道以更改时间,来自“@material-ui/pickers”的 KeyboardDateTimePicker 就会变得无响应。如何阻止这种情况?