Php Symfony 3文件上载和数据库,如果未上载新文件,则删除旧文件字段
我一直在关注Udemy上的Symfony教程,这是一个简单的CMS,我现在正在尝试扩展它 我在表单中添加了一个文件上传字段,文件被上传,文件名存储在数据库中 添加新记录的工作原理与编辑记录的工作原理相同如果我选择一个新文件在编辑表单上添加一个新文件 但是,如果我尝试编辑而不选择要上载的新文件,则原始文件名将从数据库中删除 这就是我目前在控制器中所做的Php Symfony 3文件上载和数据库,如果未上载新文件,则删除旧文件字段,php,symfony,Php,Symfony,我一直在关注Udemy上的Symfony教程,这是一个简单的CMS,我现在正在尝试扩展它 我在表单中添加了一个文件上传字段,文件被上传,文件名存储在数据库中 添加新记录的工作原理与编辑记录的工作原理相同如果我选择一个新文件在编辑表单上添加一个新文件 但是,如果我尝试编辑而不选择要上载的新文件,则原始文件名将从数据库中删除 这就是我目前在控制器中所做的 public function editAction(Request $request, Car $car) { $deleteForm
public function editAction(Request $request, Car $car)
{
$deleteForm = $this->createDeleteForm($car);
$editForm = $this->createForm('CarBundle\Form\CarType', $car);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$file = $editForm['brochure']->getData();
if(!empty($file)) {
// Generate a unique name for the file before saving it
$fileName = md5(uniqid()).'.'.$file->guessExtension();
// Move the file to the directory where brochures are stored
$file->move( $this->getParameter('brochures_directory'), $fileName );
$car->setBrochure($fileName);
} else {
$id = $car->getId();
$em = $this->getDoctrine()->getManager();
$car = $em->getRepository('CarBundle:Car')->find($id);
$fileName = $car->getBrochure();
$car->setBrochure($fileName);
}
$em = $this->getDoctrine()->getManager();
$em->merge($car);
$em->flush();
return $this->redirectToRoute('car_edit', array('id' => $car->getId()));
// return $this->redirectToRoute("car_index");
}
如果Symfony form builder我有这个
->add('brochure', FileType::class,[
'label' => 'Image',
'data_class' => null,
'required' => false
])
我认为问题来自FormBuilder data_类,由于该错误,我不得不添加该类
表单的视图数据应为class>Symfony\Component\HttpFoundation\File\File的实例,但为(n)字符串
但我不知道如何修复它,欢迎任何建议或帮助
另外,我已经读到这可能是一个服务,但婴儿第一步 的底部(ctrl+f表示“在创建表单以编辑已保存的项目时…”表示如果您正在编辑项目,最好只使用旧文件名创建一个新文件
public function editAction(Request $request, Car $car)
{
$editForm = $this->createForm('CarBundle\Form\CarType', $car);
$editForm->handleRequest($request);
$brochureDir = $this->getParameter('brochures_directory');
if (!empty($car->getBrochure()) {
$car->setBrochure(
new File($brochureDir . '/' . $car->getBrochure()
);
}
if ($editForm->isSubmitted() && $editForm->isValid()) {
$file = $car->getBrochure();
if (!empty($file)) {
// Generate a unique name for the file before saving it
$fileName = md5(uniqid()) . '.' . $file->guessExtension();
// Move the file to the directory where brochures are stored
$file->move($brochureDir, $fileName);
$car->setBrochure($fileName);
}
$em = $this->getDoctrine()->getManager();
$em->flush();
return $this->redirectToRoute('car_edit', array('id' => $car->getId()));
}
}
现在,您可以从表单中删除“'data\u class'=>null”,让它成为新文件或旧条目中的null
我还清理了一些其他的东西——去掉了你的两个条令调用,去掉了“合并”(从未见过,甚至不知道它是做什么的。如果这是一个编辑表单,实体已经存在于条令中,所以只需编辑实体和刷新就可以了)。我还去掉了else语句,因为除了设置一些变量外,我看不到它在做什么。如果这段代码不起作用,而您需要else语句(或者我删除的任何其他语句),请将它放回那里,然后再继续工作。这不是经过测试的代码
祝你好运 所以我找到了一个解决方案,感谢所有帮助过我的人的建议! 我将发布我的解决方案供其他人查看,但请注意我不是Symfony专家,因此我不能说它是否适用于Symfony或最佳实践
public function editAction(Request $request, Car $car)
{
$deleteForm = $this->createDeleteForm($car);
$editForm = $this->createForm('CarBundle\Form\CarType', $car);
//get the current file name if there is one
$currentFile = $car->getBrochure();
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$file = $editForm['brochure']->getData();
if(!empty($file)) {
//if new file has been posted, use it to update DB
// Generate a unique name for the file before saving it
$fileName = md5(uniqid()).'.'.$file->guessExtension();
// Move the file to the directory where brochures are stored
$file->move(
$this->getParameter('brochures_directory'),
$fileName
);
$car->setBrochure($fileName);
} else {
//if no new file has been posted and there is a current file use that to update the DB
if (!empty($currentFile)) {
$car->setBrochure($currentFile);
}
}
$em = $this->getDoctrine()->getManager();
$em->flush();
return $this->redirectToRoute('car_edit', array('id' => $car->getId()));
// return $this->redirectToRoute("car_index");
}
return array(
'car' => $car,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
也许可以尝试删除else块。您还设置了两次$em,这将重置它。在顶部设置$em谢谢你的建议,我永远不会在“创建表单编辑已保存的项目时”打卡。我仍然有问题,我的类无法区分提交表单中的$car->get宣传册()和数据库中的$car->get宣传册()(当没有新文件时),如果还有其他建议,我会继续尝试!您可以执行$editForm['宣传册]]操作来获取表单的版本。或者执行空检查。就像($car->get宣传册()。或者两者兼而有之。例如,如果表单为空,则执行car->get宣传册。如果宣传册为空,则获取表格等。