[英]Symfony update collection form - replace picture doesn't work
(Symfony 5) (Symfony 5)
I'm trying to update an object which have a collectionform of pictures.我正在尝试更新具有图片集合形式的 object。 Adding and removing pictures works but updating one / replacing it doesn't.添加和删除图片有效,但更新/替换图片无效。 When I try to replace the picture it add a new picture but keep the old one too.当我尝试替换图片时,它会添加一张新图片,但也会保留旧图片。 I want to replace the picture, so removing the old one and add the new one.我想替换图片,所以删除旧的并添加新的。
Controller: Controller:
/**
* @Route("/tricks/edit/{slug}", name="trick_edit")
*/
public function editTrick(Trick $trick, Request $request, FileUploader $fileUploader, Filesystem $filesystem): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$originalMediaPictures = new ArrayCollection();
// Create an ArrayCollection of the current mediaPicture objects in the database
foreach ($trick->getMediaPictures() as $mediaPicture) {
$originalMediaPictures->add($mediaPicture);
}
$form = $this->createForm(TrickFormType::class, $trick);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$slugger = new AsciiSlugger();
$trick->setSlug($slugger->slug($trick->getTitle()));
$trick->setUserEditor($this->getUser());
$trick->setEditedDate(new \DateTime('now'));
foreach ($form->get('mediaPictures') as $picture) {
$file = $picture->get('name')->getData();
if (!empty($file)) {
$trickPictureFileName = $fileUploader->upload($file, $this->getParameter('app.trick_picture_directory'));
$mediaPicture = new MediaPicture();
$mediaPicture->setTrick($trick);
$mediaPicture->setName($trickPictureFileName);
$trick->addMediaPicture($mediaPicture);
}
}
foreach ($originalMediaPictures as $mediaPicture) {
if (!$trick->getMediaPictures()->contains($mediaPicture)) {
$trick->getMediaPictures()->removeElement($trick);
$filesystem->remove($this->getParameter('app.trick_picture_directory') . $mediaPicture->getName());
}
}
foreach ($trick->getMediaPictures() as $mediaPicture) {
if (empty($mediaPicture->getName())) {
$trick->removeMediaPicture($mediaPicture);
}
}
$entityManager->persist($trick);
$entityManager->flush();
$this->addFlash('success', 'Trick updated.');
return $this->redirectToRoute('trick_detail', ['slug' => $trick->getSlug()]);
}
return $this->render('trick/edit_trick.html.twig', [
'trick' => $trick,
'trickForm' => $form->createView()
]);
}
Parent Form:父表格:
->add('mediaPictures', CollectionType::class, [
'entry_type' => MediaPictureFormType::class,
'label' => false,
'required' => false,
'mapped' => true,
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false
])
ChildForm:子窗体:
->add('name', FileType::class, [
'label' => 'Picture',
'mapped' => false,
'attr' => [
'class' => 'form-control border border-gray-300 text-gray-900 rounded hover:shadow focus:outline-none focus:bg-gray-100 focus:bg-opacity-50 focus:border-gray-400 py-2 px-4'
],
'multiple' => false,
'row_attr' => ['class' => 'flex flex-col py-5 px-8'],
'empty_data' => '',
'required' => false,
'data_class' => null,
])
Something is not right on this line:这条线有些不对劲:
$trick->getMediaPictures()->removeElement($trick);
You are trying to remove the Trick
from its own collection of MediaPictures
(it's not in there).您正试图从它自己的MediaPictures
集合中删除Trick
(它不在其中)。
Maybe you mean to do something like this:也许你的意思是做这样的事情:
foreach ($originalMediaPictures as $mediaPicture) {
if (!$trick->getMediaPictures()->contains($mediaPicture)) {
// $trick->getMediaPictures()->removeElement($trick);
// remove association from $mediaPicture
$mediaPicture->setTrick(null);
$entityManager->persist($mediaPicture);
// and/or delete $mediaPicture from database
$entityManager->remove($mediaPicture);
$filesystem->remove($this->getParameter('app.trick_picture_directory') . $mediaPicture->getName());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.