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() }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥20 基于python进行多背包问题的多值编码
    • ¥15 相同型号电脑与配置,发现主板有一台貌似缺少了好多元器件似的,会影响稳定性和使用寿命吗?
    • ¥15 要求编写稀疏矩阵A的转置矩阵的算法
    • ¥15 编写满足以下要求的停车场管理程序,设停车场只有一个可停放n辆车的狭窄通道且只有一个大门可供车辆进出。
    • ¥15 C语言:数据子序列基础版
    • ¥20 powerbulider 导入excel文件,显示不完整
    • ¥15 用keil调试程序保证结果进行led相关闪烁
    • ¥15 paddle训练自己的数据loss降不下去
    • ¥20 用matlab的pdetool解决以下三个问题
    • ¥15 单个福来轮的平衡与侧向滑动是如何做到的?