Symfony:使用相同格式

时间:2015-11-11 13:18:13

标签: forms symfony collections

我有一个附件表单类型,如下所示:

$builder
    ->add('file_upload', 'file', array('required' => false,
                                       'mapped'   => false,
                                       'label'    => false))
    ->add('filename', 'text', array('label' => 'Name'))
    ->add('comment', 'textarea', array('label' => 'Comment'))
;

我有一个主要实体,它有一系列附件,如下所示:

->add('attachments', 'collection', array('type'         => new AttachmentType(),
                                              'allow_add'    => true,
                                              'allow_delete' => true,
                                              'by_reference' => false
                                             ))

在我的 MainEntityController 中,我想循环浏览上传的文件,我希望能够将每个文件与其对应的名称和注释相关联,这可能吗?我甚至无法遍历文件,因为我真的不知道如何调试表单(file_upload字段没有出现在探查器中。)

这是我失败的控制器代码:

$attachments = $editForm['attachments']->getData();

foreach ($attachments as $attachment)
{
    $ficheros = $attachment['file_upload']->getData();

    foreach ($ficheros as $fich)
    {
         error_log("Upload " . $fich->getClientOriginalName());

我收到错误:

  

错误:无法使用AppBundle \ Entity \ Attachment类型的对象作为数组   $ ficheros = $ attachment ['file_upload'] - > getData();

所以,

如何循环上传文件? 我可以将每个上传的文件与相应的名称和评论联系起来吗? 或者我应该选择其他解决方案,比如文件上传和ajax文件?

0 个答案:

没有答案