我有三个字段id,image,title的实体。数据库中的所有字段都不为null.image字段用于存储文件路径。当我创建新记录时,它工作正常,图像上传到公共文件夹中。但是在编辑时记录图像字段为null,并且此值不应为null消息出现。图像字段未设置,并且在表单提交中始终为null。
我关注symfony文档https://symfony.com/doc/current/controller/upload_file.html
实体字段
/**
* @ORM\Column(type="string", length=255)
*
*/
private $image;
表单类型
public function buildForm(FormBuilderInterface $builder, array
$options)
{
$builder->add('title', TextType::class, [
"label" => "Title"
])
->add('image', FileType::class, [
'data_class' => null,
])
->add('save', SubmitType::class, array("label" => "Save"));
}
控制器
public function editAction(Request $request, Gallery $gallery)
{
$em = $this->getDoctrine()->getManager();
$file = new File($this->getParameter('gallery_directory') . '/' .
$gallery->getImage());
$gallery->setImage($file);
$form = $this->createForm(GalleryType::class, $gallery);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$file = $form->get('image')->getData();
$fileName = $this->generateUniqueFileName() . '.' . $file->guessExtension();
// Move the file to the directory where brochures are stored
try {
$file->move(
$this->getParameter('brochures_directory'),
$fileName
);
} catch (FileException $e) {
}
$gallery->setImage($fileName);
$em->persist($gallery);
$em->flush();
}
return $this->render('Gallery/add.html.twig', [
'form' => $form->createView(),
'gallery' => $gallery,
]);
}
答案 0 :(得分:0)
我无法获得在https://symfony.com/doc/current/controller/upload_file.html中上传文件的示例也无法正常工作。 我修改了您的代码,使其看起来更像是我必须工作的代码。以下代码尚未经过测试,但可以使您更接近工作。
实体字段:
/**
* @ORM\Column(type="string", length=255)
*
*/
private $image;
// Note there is no type hint on the return value.
// Sometimes it is a string, sometimes a UploadedFile.
// php bin/console make:entity adds a type hint which must be removed.
public function getImage()
{
return $this->image;
}
public function setImage($image): self
{
$this->image = $image;
return $this;
}
用于创建实体的表单类型:
public function buildForm(FormBuilderInterface $builder, array
$options)
{
$builder->add('title', TextType::class, [
"label" => "Title"
])
->add('image', FileType::class)
->add('save', SubmitType::class, array("label" => "Save"));
}
GalleryEditType表单类型,用于编辑实体: (我不知道如何制作用于创建/编辑的组合表格。)
public function buildForm(FormBuilderInterface $builder, array
$options)
{
$builder->add('title', TextType::class, [
"label" => "Title"
])
->add('image', FileType::class, [
'required' => false,
'mapped' => false
])
->add('save', SubmitType::class, array("label" => "Save"));
}
控制器: (对此代码我不太确定,但是您肯定需要下面的UploadedFile类型提示。)
public function editAction(Request $request, Gallery $gallery)
{
$em = $this->getDoctrine()->getManager();
$form = $this->createForm(GalleryEditType::class, $gallery);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// $file stores the uploaded image file.
/** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
$file = $form->get('image')->getData();
if ($file != null) {
// The user selected a new image.
$fileName = $this->generateUniqueFileName() . '.' . $file->guessExtension();
// Move the file to the directory where brochures are stored
try {
$file->move(
$this->getParameter('brochures_directory'),
$fileName
);
} catch (FileException $e) {
}
// Update the image property to store the image file name instead of its contents.
$gallery->setImage($fileName);
}
$em->persist($gallery);
$em->flush();
return $this->redirectToRoute('gallery_index', [
'id' => $gallery->getId(),
]);
}
return $this->render('Gallery/edit.html.twig', [
'form' => $form->createView(),
'gallery' => $gallery,
]);
}
请在工作后发布更新的代码。谢谢!