如何在Symfony表单主题中访问CollectionType值?

时间:2017-09-04 06:17:15

标签: php symfony

我正在使用symfony-collection plugin来管理CollectionType表单类型,并且在我创建实体时它工作正常。

但是如果我想在我的模板中访问我的Collection数据的某些值来更新实体(就像图像一样,为了显示它),我收到此错误:Impossible to access an attribute ("imageName") on a null variable.

我设法转储了像{{ dump(form.vars.value) }}这样的表单值,它返回了我的Collection的实体,如下所示:

ArticleAttachment {#3220 ▼
  -id: 6
  -imageFile: null
  -imageName: "59a68c78cca5c_2017-08-24 18.40.39-1.jpg"
  -imageUpdatedAt: DateTime {#5439 ▼
    +"date": "2017-08-30 09:59:20.000000"
    +"timezone_type": 3
    +"timezone": "UTC"
  }

但是,如果我想要imageName(或任何其他属性),例如{{ dump(form.vars.value.imageName) }},我会收到上述错误。表格的其余部分没问题。

有任何线索吗?

这是我的表单主题:

{% block attachment_label %}{% endblock %}

{% block attachment_widget %}
    <div class="row">
        <div class="col-md-4">
            {{ dump(form.vars.value.imageName) }}
        </div>
        <div class="col-md-2">
            {{ form_widget(form.imageFile) }}
            {{ form_widget(form.position) }}
            <p class="text-danger">{{ form_errors(form.imageFile) }}</p>
        </div>
        <div class="col-md-1">
            <a href="#" class="collection-up btn btn-default" title="Move fruit up"><span class="glyphicon glyphicon-arrow-up"></span></a>
            <a href="#" class="collection-down btn btn-default" title="Move fruit down"><span class="glyphicon glyphicon-arrow-down"></span></a>
        </div>
        <div class="col-md-1">
            <a href="#" class="collection-remove btn btn-default" title="Delete fruit"><span class="glyphicon glyphicon-trash"></span></a>
            <a href="#" class="collection-add btn btn-default" title="Add fruit"><span class="glyphicon glyphicon-plus-sign"></span></a>
        </div>
        <div class="col-md-1">
            <a href="#" class="collection-duplicate btn btn-default" title="Duplicate fruit"><span class="glyphicon glyphicon-th-large"></span></a>
        </div>
    </div>
{% endblock %}

0 个答案:

没有答案