doushi6864 2018-08-10 08:18
浏览 122
已采纳

访问twig上数组内的对象属性

The template has this data(the result of {{ dump(extra) }})

array (size=4)
  0 => 
    object(MyProject\Entity\Translation)[210]
      private 'language' => 
        object(Proxies\__CG__\MyProject\Entity\Language)[225]
          public '__initializer__' => null
          public '__cloner__' => null
          public '__isInitialized__' => boolean true
          private 'code' (MyProject\Entity\Language) => string 'es' (length=2)
          private 'id' (MyProject\Entity\Language) => int 70
      private 'entity' => string 'Reason' (length=6)
      private 'entityId' => int 10000
      private 'field' => string 'text' (length=4)
      private 'text' => string 'No quiero recibir emails' (length=24)
  1 => 
    object(MyProject\Entity\Translation)[224]
      private 'language' => 
        object(Proxies\__CG__\MyProject\Entity\Language)[225]
          public '__initializer__' => null
          public '__cloner__' => null
          public '__isInitialized__' => boolean true
          private 'code' (MyProject\Entity\Language) => string 'es' (length=2)
          private 'id' (MyProject\Entity\Language) => int 70
      private 'entity' => string 'Reason' (length=6)
      private 'entityId' => int 10001
      private 'field' => string 'text' (length=4)
      private 'text' => string 'No me gusta la web' (length=18)
  2 => 
    object(MyProject\Entity\Translation)[223]
      private 'language' => 
        object(Proxies\__CG__\MyProject\Entity\Language)[225]
          public '__initializer__' => null
          public '__cloner__' => null
          public '__isInitialized__' => boolean true
          private 'code' (MyProject\Entity\Language) => string 'es' (length=2)
          private 'id' (MyProject\Entity\Language) => int 70
      private 'entity' => string 'Reason' (length=6)
      private 'entityId' => int 10002
      private 'field' => string 'text' (length=4)
      private 'text' => string 'No tengo ningún motivo' (length=23)
  3 => 
    object(MyProject\Entity\Translation)[221]
      private 'language' => 
        object(Proxies\__CG__\MyProject\Entity\Language)[225]
          public '__initializer__' => null
          public '__cloner__' => null
          public '__isInitialized__' => boolean true
          private 'code' (MyProject\Entity\Language) => string 'es' (length=2)
          private 'id' (MyProject\Entity\Language) => int 70
      private 'entity' => string 'Reason' (length=6)
      private 'entityId' => int 10003
      private 'field' => string 'text' (length=4)
      private 'text' => string 'Otros' (length=5)

But I'm trying to access to the object properties but the options in the select are not printed.

{{ dump(extra) }}
<select id="motivos_baja" name="motivos_baja" class="form-control">
      <option value="0" selected>Seleccione un motivo</option>
            {% for extra in reason%}
             <option value="{{ reason.getEntityId() }}">{{ reason.getText() }}</option>
             {% endfor %}    
 </select>

The objects are doctrine entities.

  • 写回答

1条回答 默认 最新

  • douhuanqiao5290 2018-08-10 08:23
    关注

    First, you must fix the loop; in Twig, the for loop works as for item in list:

    {% for reason in extra %}
        {# ... #}
    {% endfor %}    
    

    Second, it looks like the actual reason is a property of the MyProject\Entity\Translation object. I assume there is some getter for the entity, therefore you would access the reason text with:

    {{ reason.getEntity().getText() }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入