我无法克隆数组集合对象,因为它表示它不是对象。
这是我的行动:
public function editAction(Request $request, Post $post)
{
$deleteForm = $this->createDeleteForm($post);
$editForm = $this->createForm('AppBundle\Form\PostType', $post);
$editForm->handleRequest($request);
var_dump($post->getPostImages()); // object
// $original_images = clone $post->getPostImages(); says not object
$original_images = $post->getPostImages();
if ($editForm->isSubmitted() && $editForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$postImages = $post->getPostImages();
$post->setPostImages([]);
var_dump($original_images); // not same as 1st dump
die;
foreach ($postImages as $image) {
$fileName = md5(uniqid()).'.'.$image->guessExtension();
if ($image->move(
$this->container->getParameter('post_image').$post->getTitle(),
$fileName)) {
// $images[] = $fileName;
$post->addPostImage(array('fileName' => $fileName));
}
}
$em->persist($post);
$em->flush();
return $this->redirectToRoute('post_edit', array('id' => $post->getId()));
}
if ($this->checkAction($post)) {
return $this->checkAction($post);
}
return $this->render('post/edit.html.twig', array(
'post' => $post,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
第一个var转储返回:
object(Doctrine\Common\Collections\ArrayCollection)#380 (1) { ["elements":"Doctrine\Common\Collections\ArrayCollection":private]=> array(1) { [0]=> array(1) { ["fileName"]=> string(36) "ba2a3dd44902cc4411e6508015f026a3.png" } } }
当我尝试克隆对象时,我得到了这个:
Error: __clone method called on non-object
第二个var转储返回此信息(提交表单后):
array(1) {
[0]=>
object(Symfony\Component\HttpFoundation\File\UploadedFile)#14 (7) {
["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
bool(false)
["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
string(34) "Screenshot_2016-06-20_14-28-36.png"
["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
string(9) "image/png"
["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
int(610945)
["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
int(0)
["pathName":"SplFileInfo":private]=>
string(14) "/tmp/phpoY3L5i"
["fileName":"SplFileInfo":private]=>
string(9) "phpoY3L5i"
}
}
更新#1: 表单构建器:
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\CallbackTransformer;
class PostType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('content')
->add('postImages', FileType::class, array('label' => 'Post Image', 'multiple' => true, 'data_class' => 'Doctrine\Common\Collections\ArrayCollection', 'required' => false))
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Post'
));
}
}
答案 0 :(得分:1)
显然感动:
$original_images = $post->getPostImages();
以上:
$editForm->handleRequest($request);
诀窍,现在代码看起来像这样:
$deleteForm = $this->createDeleteForm($post);
$editForm = $this->createForm('AppBundle\Form\PostType', $post);
$original_images = $post->getPostImages();
$editForm->handleRequest($request);