可上传扩展
在使用此扩展之前,请阅读官方的 Uploadable 文档。 一切就绪后,像往常一样使用 Form 组件。 然后,在验证表单有效后,执行以下操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
$document = new Document();
$form = $this->createFormBuilder($document)
->add('name')
->add('myFile')
->getForm()
;
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($document);
$uploadableManager = $this->get('stof_doctrine_extensions.uploadable.manager');
// Here, "getMyFile" returns the "UploadedFile" instance that the form bound in your $myFile property
$uploadableManager->markEntityToUpload($document, $document->getMyFile());
$em->flush();
return $this->redirect($this->generateUrl('...'));
}
return $this->render('...', array('form' => $form->createView()));
就是这样。 Uploadable 扩展会处理剩下的事情。 记得阅读它的文档!
这项工作,包括代码示例,均根据 Creative Commons BY-SA 3.0 许可获得许可。