[英]Symfony 6: embedded form Collection with File upload in child form
I have an entity ClientFileAction
which is parent to an entity Attachment
in OneToMany
relation.我有一个实体
ClientFileAction
,它是OneToMany
关系中实体Attachment
的父级。 Attachment
holds not only file path, but also information about files, like title, upload date, etc. Attachment
不仅包含文件路径,还包含有关文件的信息,如标题、上传日期等。
Attachment:
#[ORM\Column(type: 'string', length: 255)]
private $title;
#[ORM\Column(type: 'datetime')]
private $uploaded;
#[ORM\Column(type: 'string', length: 255)]
private $filePath;
When it comes to create a form type AttachmentType
and upload files one to one , there is no problem:在创建表单类型
AttachmentType
和一对一上传文件时,没有问题:
AttachmentType:
$builder
->add('title', null, ['label' => 'Title', 'required' => true])
->add('attachmentFile', FileType::class, [
'label' => 'File',
'mapped' => false,
'required' => true,
'constraints' => [
new File([
'maxSize' => '1024k',
])
],
]);
In the controller I just get uploaded file with $attachmentFile = $form->get('attachmentFile')->getData();
在 controller 中,我刚刚使用
$attachmentFile = $form->get('attachmentFile')->getData();
and then proceed to the usual UploadedFile::move()
stuff.然后继续执行通常的
UploadedFile::move()
。
PROBLEM: EMBED AttachmentType IN PARENT FORM问题:在父表单中嵌入 AttachmentType
But when I try to upload multiple attachments (not only files, but attachments with a title field), the uploaded file field seems to be unreachable.但是当我尝试上传多个附件时(不仅是文件,还有带有标题字段的附件),上传的文件字段似乎无法访问。
ClientFileActionType:
$builder
->add('description', null, ['label' => 'Description', 'required' => true])
->add('attachments', CollectionType::class, ['label' => false,
'allow_add' => true,
'by_reference' => false,
'entry_type' => AttachmentType::class,
'entry_options' => ['label' => false],
]);
When I embed the AttachmentType
as Collection inside ClientFileActionType
, then, in the controller I don't find a way to get uploaded files:当我将
AttachmentType
作为Collection嵌入到ClientFileActionType
中时,在 controller 中我找不到获取上传文件的方法:
$attachments = $form->get('attachments')->getData();
$attachments
is an array of Attachment
, and, as attachmentFile
is not a mapped field, it dissapeared on the $form->handleRequest($request);
$attachments
是Attachment
的数组,并且由于attachmentFile
不是映射字段,它在$form->handleRequest($request);
. .
I need a way to get unmapped attachmentFile
fields of the child forms someway, something like:我需要一种方法来获取子 forms 的未映射的
attachmentFile
字段,例如:
$attachmentFiles = $form->get('attachments.attachmentFile')->getData();
That throws an error.这会引发错误。 Is there a correct way to do that?
有正确的方法吗?
I found the correct way to do it as I was typing the question.我在输入问题时找到了正确的方法。
The uploaded files are in the Request
object, so a correct approach for this scenario of file upload management could be:上传的文件在
Request
object中,所以这种文件上传管理的正确做法应该是:
if ($form->isSubmitted() && $form->isValid())
{
$i=0;
$files = $request->files->all('client_file_action')['attachments'];
foreach ($files as $file)
{
$attachmentFile = $file['attachmentFile'];
$originalFilename = pathinfo($attachmentFile->getClientOriginalName(), PATHINFO_FILENAME);
$safeFilename = $slugger->slug($originalFilename);
$newFilename = $safeFilename.'-' . uniqid() . '.' .$attachmentFile->guessExtension();
$attachmentFile->move('path/to/folder',$newFilename);
$attachment = $clientFileAction->findAttachment($i);
if ($attachment != null)
$attachment->setFilePath('path/to/folder/' . $newFilename);
$i++;
}
$clientFileActionRepository->add($clientFileAction, true);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.