Mysql 如何将文件添加到数据库my sql和symfony 5
我正在尝试将文件上载到我的表单我的上载文件有问题,出现以下错误: SQLSTATE[23000]:完整性约束冲突:1048列 “文件名”不能为空 这是我的代码: 控制员:Mysql 如何将文件添加到数据库my sql和symfony 5,mysql,file-upload,symfony5,Mysql,File Upload,Symfony5,我正在尝试将文件上载到我的表单我的上载文件有问题,出现以下错误: SQLSTATE[23000]:完整性约束冲突:1048列 “文件名”不能为空 这是我的代码: 控制员: /** * @Route("/new", name="demande_o_new", methods={"GET","POST"}) */ public function new(Request $request): Response {
/**
* @Route("/new", name="demande_o_new", methods={"GET","POST"})
*/
public function new(Request $request): Response
{
$demandeO = new DemandeO();
$form = $this->createForm(DemandeOType::class, $demandeO);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$brochureFile = $form->get('brochure')->getData();
// this condition is needed because the 'brochure' field is not required
// so the PDF file must be processed only when a file is uploaded
if ($brochureFile) {
$originalFilename = pathinfo($brochureFile->getClientOriginalName(), PATHINFO_FILENAME);
dd($originalFilename );
// this is needed to safely include the file name as part of the URL
$safeFilename = $slugger->slug($originalFilename);
$newFilename = $safeFilename.'-'.uniqid().'.'.$brochureFile->guessExtension();
// Move the file to the directory where brochures are stored
try {
$brochureFile->move(
$this->getParameter('brochures_directory'),
$newFilename
);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
echo "file mat3adech";
}
// updates the 'brochureFilename' property to store the PDF file name
// instead of its contents
$demandeO->setBrochureFilename($newFilename);
}
$entityManager->persist($demandeO);
$entityManager->flush();
return $this->redirectToRoute('demande_o_index');
}
return $this->render('demande_o/new.html.twig', [
'demande_o' => $demandeO,
'form' => $form->createView(),
]);
}
需求类型:
->add('brochure', FileType::class, [
'label' => 'Brochure (PDF file)',
// unmapped means that this field is not associated to any entity property
'mapped' => false,
// make it optional so you don't have to re-upload the PDF file
// every time you edit the Product details
'required' =>true,
// unmapped fields can't define their validation using annotations
// in the associated entity, so you can use the PHP constraint classes
'constraints' => [
new File([
'maxSize' => '1M',
'mimeTypes' => [
'application/pdf',
'application/x-pdf',
],
'mimeTypesMessage' => 'Please upload a valid PDF document',
])
],
])
services.yaml中的配置:
brockhures\u目录:'%kernel.project\u dir%/public/uploads/brockhures'
我在公用文件夹中添加了两个文件夹
上传->小册子