Symfony 3.4:在树枝中渲染多个数组

时间:2018-09-13 08:39:36

标签: php arrays symfony twig

我得到了一个带有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
}

应将其作为表格呈现在树枝模板中,其中许多数据是使用简单的数组访问方式(如{{ item.string }})检索的。 名为floor, ambient, name, pp, pl, pt的字段应显示在该列中,项目显示为一行(您应在此示例中看到,所有元素都包含5个键)。

我曾尝试像往常一样访问数组,但遇到与尝试访问实际上是整数的键有关的错误(不可以吗?)

这是不起作用的循环的树枝:

{% 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值,{{1} } value`,第二列,依此类推。

有人会找到正确呈现它的解决方案吗?

1 个答案:

答案 0 :(得分: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 %}