我的管理界面中有一个表单允许我通过上传图像发布文章,我有另一个表单允许我编辑文章。
当我修改我的文章时,我想保留前一个图像,如果该字段为空(所以" null")或通过删除上一个图像(如果有的话)实例化我的新图像,但是我没有成功..
目前,如果我修改我的文章,将我的字段图像留空,则doctrine会将我的字段image_id定义为数据库中的NULL。
我有一个实体" Image"与我的实体OneToOne相关"文章"和EditArticleType,它将当前项的数据设置为其默认值。
我的控制器:
public function modifierArticleAction(Article $article, Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$form = $this->createForm(EditArticleType::class, $article);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// $file récupère l'image
$file = $article->getImage();
$file2 = $article->getImage2();
$file3 = $article->getImage3();
$file4 = $article->getImage4();
$file5 = $article->getImage5();
$file6 = $article->getImage6();
$file7 = $article->getImage7();
$file8 = $article->getImage8();
$file9 = $article->getImage9();
$file10 = $article->getImage10();
if($file !== null && ($file instanceof UploadedFile) === true) {
$fileName = md5(uniqid('img_', true));
$fileExtension = $file->guessExtension();
$nomImageCompletFile = $fileName . '.' . $fileExtension;
$file->move(
$this->getParameter('articles_images_directory'),
$nomImageCompletFile
);
setKey('xxx');
$tinified = fromFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile);
$tinified->toFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile);
$image = new Image();
$image->setExtension($fileExtension);
$image->setFile($fileName);
$em->persist($image);
$article->setImage($image);
}
if($file2 !== null && ($file2 instanceof UploadedFile) === true) {
$file2Name = md5(uniqid('img_', true));
$file2Extension = $file2->guessExtension();
$nomImageCompletFile2 = $file2Name . '.' . $file2Extension;
$file2->move(
$this->getParameter('articles_images_directory'),
$nomImageCompletFile2
);
setKey('xxx');
$tinified = fromFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile2);
$tinified->toFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile2);
$image2 = new Image();
$image2->setExtension($file2Extension);
$image2->setFile($file2Name);
$em->persist($image2);
$article->setImage2($image2);
}
if($file3 !== null && ($file3 instanceof UploadedFile) === true) {
$file3Name = md5(uniqid('img_', true));
$file3Extension = $file3->guessExtension();
$nomImageCompletFile3 = $file3Name . '.' . $file3Extension;
$file3->move(
$this->getParameter('articles_images_directory'),
$nomImageCompletFile3
);
setKey('xxx');
$tinified = fromFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile3);
$tinified->toFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile3);
$image3 = new Image();
$image3->setExtension($file3Extension);
$image3->setFile($file3Name);
$em->persist($image3);
$article->setImage3($image3);
}
if($file4 !== null && ($file4 instanceof UploadedFile) === true) {
$file4Name = md5(uniqid('img_', true));
$file4Extension = $file4->guessExtension();
$nomImageCompletFile4 = $file4Name . '.' . $file4Extension;
$file4->move(
$this->getParameter('articles_images_directory'),
$nomImageCompletFile4
);
setKey('xxx');
$tinified = fromFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile4);
$tinified->toFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile4);
$image4 = new Image();
$image4->setExtension($file4Extension);
$image4->setFile($file4Name);
$em->persist($image4);
$article->setImage4($image4);
}
if($file5 !== null && ($file5 instanceof UploadedFile) === true) {
$file5Name = md5(uniqid('img_', true));
$file5Extension = $file5->guessExtension();
$nomImageCompletFile5 = $file5Name . '.' . $file5Extension;
$file5->move(
$this->getParameter('articles_images_directory'),
$nomImageCompletFile5
);
setKey('xxx');
$tinified = fromFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile5);
$tinified->toFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile5);
$image5 = new Image();
$image5->setExtension($file5Extension);
$image5->setFile($file5Name);
$em->persist($image5);
$article->setImage5($image5);
}
if($file6 !== null && ($file6 instanceof UploadedFile) === true) {
$file6Name = md5(uniqid('img_', true));
$file6Extension = $file6->guessExtension();
$nomImageCompletFile6 = $file6Name . '.' . $file6Extension;
$file6->move(
$this->getParameter('articles_images_directory'),
$nomImageCompletFile6
);
setKey('xxx');
$tinified = fromFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile6);
$tinified->toFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile6);
$image6 = new Image();
$image6->setExtension($file6Extension);
$image6->setFile($file6Name);
$em->persist($image6);
$article->setImage6($image6);
}
if($file7 !== null && ($file7 instanceof UploadedFile) === true) {
$file7Name = md5(uniqid('img_', true));
$file7Extension = $file7->guessExtension();
$nomImageCompletFile7 = $file7Name . '.' . $file7Extension;
$file7->move(
$this->getParameter('articles_images_directory'),
$nomImageCompletFile7
);
setKey('xxx');
$tinified = fromFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile7);
$tinified->toFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile7);
$image7 = new Image();
$image7->setExtension($file7Extension);
$image7->setFile($file7Name);
$em->persist($image7);
$article->setImage7($image7);
}
if($file8 !== null && ($file8 instanceof UploadedFile) === true) {
$file8Name = md5(uniqid('img_', true));
$file8Extension = $file8->guessExtension();
$nomImageCompletFile8 = $file8Name . '.' . $file8Extension;
$file8->move(
$this->getParameter('articles_images_directory'),
$nomImageCompletFile8
);
setKey('xxx');
$tinified = fromFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile8);
$tinified->toFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile8);
$image8 = new Image();
$image8->setExtension($file8Extension);
$image8->setFile($file8Name);
$em->persist($image8);
$article->setImage8($image8);
}
if($file9 !== null && ($file9 instanceof UploadedFile) === true) {
$file9Name = md5(uniqid('img_', true));
$file9Extension = $file9->guessExtension();
$nomImageCompletFile9 = $file9Name . '.' . $file9Extension;
$file9->move(
$this->getParameter('articles_images_directory'),
$nomImageCompletFile9
);
setKey('xxx');
$tinified = fromFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile9);
$tinified->toFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile9);
$image9 = new Image();
$image9->setExtension($file9Extension);
$image9->setFile($file9Name);
$em->persist($image9);
$article->setImage9($image9);
}
if($file10 !== null && ($file10 instanceof UploadedFile) === true) {
$file10Name = md5(uniqid('img_', true));
$file10Extension = $file10->guessExtension();
$nomImageCompletFile10 = $file10Name . '.' . $file10Extension;
$file10->move(
$this->getParameter('articles_images_directory'),
$nomImageCompletFile10
);
setKey('xxx');
$tinified = fromFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile10);
$tinified->toFile($this->getParameter('articles_images_directory') . '/' . $nomImageCompletFile10);
$image10 = new Image();
$image10->setExtension($file10Extension);
$image10->setFile($file10Name);
$em->persist($image10);
$article->setImage10($image10);
}
$slugify = new Slugify();
$article->setTitre($article->getTitre());
$article->setIntroduction($article->getIntroduction());
$article->setContenu($article->getContenu());
$article->setSlug($slugify->slugify($article->getTitre()));
$em->flush();
$this->addFlash('success', 'Votre article a bien été modifié :)');
return $this->redirectToRoute('admin_sg_modifier_article', ['id' => $id]);
}
return $this->render(
'AdminSGBundle:page:modifier-article.html.twig', [
'form' => $form->createView(),
'articleToUpdate' => $article,
]
);
}
提前感谢您的帮助
答案 0 :(得分:0)
在你的文章中,实体尝试编辑每个图像字段:
public function setImage($image)
{
if (!is_null($image)) {
$this->image = $image;
}
return $this;
}
...
public function setImage10($image)
{
if (!is_null($image)) {
$this->image10 = $image;
}
return $this;
}
如果为null,则此逻辑必须阻止设置图像实体。