dora12345678 2013-06-14 11:06
浏览 28
已采纳

Twig不会在对象中显示数据

I'm trying to use Twig to display a simple list of Doctrine 2 entity objects. The objects are fetched with the untouched results from a DQL query. The PHP code is like this:

/*
 * I know the query in the Repository class works as I get the correct objects
 * from the method and can iterate through them with foreach in PHP, displaying
 * data from the object through getters, like ->getId()
 */
$received = $repo->getReceived($id);

/*
 * This appears to work, the variables are assigned correctly.
 */
echo $template->render(array("received" => $received, "first" => $received[0]));

The Twig template contains this:

{% for item in received %}
    <li>Item: {{ item.id }}, {{ first.id }}</li>
{% endfor %}

The strange thing is that twig iterates through the received array correctly, there are three objects in the array and there are three lines output but the id (or any other member) doesn't come out when accessed as item.id but the second output, first.id, comes out correctly.

What I should be seeing is this:

Item 1, 1
Item 2, 1
Item 3, 1

but instead I'm seeing:

Item , 1
Item , 1
Item , 1

I've tried different variations to get the output but nothing has worked so far, item.getId, item.getId().

Any ideas what I am missing here?

Edit dump output

{{ dump(received) }} returns the following:

array(3) {
  [0]=>
  object(Received)#219 (6) {
    ["id":"Received":private]=>
    int(1)
  ... snip ...
  }
  [1]=>
  object(Received)#208 (6) {
    ["id":"Received":private]=>
    int(2)
  ... snip ...
  }
  [2]=>
  object(Received)#210 (6) {
    ["id":"Received":private]=>
    int(3)
  ... snip ...

I've cut it down, these are three full blown Doctrine entity objects so they are in the array and look correct.

  • 写回答

1条回答 默认 最新

  • dongxi3859 2013-06-24 13:54
    关注

    I've finally found a way around this although I can't understand why I need to do it. I changed the array to contain a set of arrays instead of objects but that didn't work either.

    I then created an alternative array for testing with the exact same structure and that array worked without any problems. Anyone have any idea what might cause one array to work while the other fails? Both are two dimensional arrays.

    So what I ended up with was that I used the two dimensional array and changed the twig code to:

    {% for item in received %}
        <li>Item: {{ attribute(item, 'id') }}</li>
    {% endfor %}
    

    For some reason attribute() is needed there although I did try that previously but then Twig complained that attribute() didn't exist.

    Hopefully this will help some others.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)