Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2:don';t更新数据库中的图像,如果它是';它是空的_Php_Symfony_Symfony 2.8 - Fatal编程技术网

Php Symfony2:don';t更新数据库中的图像,如果它是';它是空的

Php Symfony2:don';t更新数据库中的图像,如果它是';它是空的,php,symfony,symfony-2.8,Php,Symfony,Symfony 2.8,我有两个字段logo和image,用于存储图像。在db中,它们表示为字符串 /** * @ORM\PrePersist */ public function preUpload() { if ($this->logo !== null) { $image_name = uniqid().'.'.$this->logo->guessExtension(); if($image

我有两个字段logo和image,用于存储图像。在db中,它们表示为字符串

   /**
     * @ORM\PrePersist
     */
    public function preUpload()
    {
        if ($this->logo !== null) {
            $image_name = uniqid().'.'.$this->logo->guessExtension();
            if($image_name) {
                $this->logo->move($this->getUploadRootDir(), $image_name);
                unset($this->logo);
                $this->logo = $image_name;
            }
            else{
                unset($this->logo);
            }
        }
        if(is_array($this->images) && !empty($this->images)){
            $images = array();
            foreach($this->images as $image){
                if($image) {
                    $image_name = uniqid() . '.' . $image->guessExtension();
                    $image->move($this->getUploadRootDir(), $image_name);
                    $images[] = $image_name;
                }
            }
            if($images){
                $this->images = json_encode($images);
            }
        }
    }
好的.orm.yml

lifecycleCallbacks:
            prePersist: [ preUpload, setCreatedAtValue]
            preUpdate: [ preUpload, setUpdatedAtValue ]
在这里,我将图像移动到正确的文件夹中,并在分配此vars字符串值之后。它工作得很好。但我有一个问题。如果我更新另一个字段,并且不上传新图像,我将在db中有空的图像字段。Symfony将删除此字段。我怎样才能改变这种行为

创建形式:

       /**
         * {@inheritdoc}
         */
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('name')
                ->add('logo', FileType::class, array('required' => false, 'data_class' => null))
                ->add('description', TextareaType::class)
                ->add('images', FileType::class, array('attr' => array("multiple" => "multiple"), 'data_class' => null, 'required' => false))
                ->add('price')
                ->add('is_active')
                ->add('category');
        }
编辑操作

/**
 * Displays a form to edit an existing good entity.
 *
 */
public function editAction(Request $request, Good $good)
{
    $deleteForm = $this->createDeleteForm($good);
    $editForm = $this->createForm('Shop\ShopBundle\Form\GoodType', $good);
    $editForm->handleRequest($request);
    if ($editForm->isSubmitted() && $editForm->isValid()) {
        $this->getDoctrine()->getManager()->flush();
        //return $this->redirectToRoute('app_good_edit', array('id' => $good->getId()));
    }
    $form_edit = $editForm->createView();

    //\Doctrine\Common\Util\Debug::dump($form_edit->children['images']->vars);

    $form_edit->children['images']->vars = array_replace($form_edit->children['images']->vars, array('full_name' => 'shop_shopbundle_good[images][]'));
    return $this->render('ShopShopBundle:good:edit.html.twig', array(
        'good' => $good,
        'edit_form' => $form_edit,
        'delete_form' => $deleteForm->createView(),
    ));
}

一个简单的解决方法是检索,在表单的
image
字段为empy的情况下,是db中的旧值。 大概是这样的:

if ($editForm->isSubmitted() && $editForm->isValid()) {
    $goodSubmitted = $editForm->getData();
    if(empty($goodSubmitted->getImages()) {
        $goodSaved = $this->getDoctrine()->getManager()->getRepository("good repository name here")->find($goodSubmitted->getId());
        $goodSubmitted->setImages($goodSaved->getImages());
    }
    $this->getDoctrine()->getManager()->flush();
}
我不知道你的实体结构,这只是一个想法

如果您想编辑以添加图像,您也必须处理该问题

然而,这可能是痛苦的。我的建议是保持简单:为所有图像创建一个实体,并通过适当的关系链接到您的
Good
实体。
我发现在描述上述结构时

我尝试了第一种方法,但是
$goodSaved->getImages())
总是返回null,尽管db中有这样的值。当然,我会尝试第二种方法,但我想完成第一种方法。