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 Qt4代码实现下面的界面
  • ¥15 prism提示我reinstall prism 如何解决
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法
  • ¥20 while循环中OLED显示中断中的数据不正确
  • ¥15 这个视频里的stm32f4代码是怎么写的
  • ¥15 JNA调用DLL报堆栈溢出错误(0xC00000FD)
  • ¥15 请教SGeMs软件的使用
  • ¥15 自己用vb.net编写了一个dll文件,如何只给授权的用户使用这个dll文件进行打包编译,未授权用户不能进行打包编译操作?
  • ¥50 深度学习运行代码直接中断
  • ¥20 需要完整的共散射点成像代码