访问对象属性在树枝上插入了一个Arary

时间:2018-08-10 08:18:21

标签: php doctrine twig

模板具有此数据({{ 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)

但是我试图访问对象属性,但是选择中的选项未打印。

{{ 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>

对象是doctrine实体。

1 个答案:

答案 0 :(得分:0)

首先,您必须修复循环;在Twig中,for循环用作for item in list

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

第二,看起来实际原因是MyProject\Entity\Translation对象的属性。我假设该实体有一些吸气剂,因此您可以使用以下方式访问原因文本:

{{ reason.getEntity().getText() }}