Symfony2:如果没有,请不要更新db中的图像

时间:2017-06-05 08:46:31

标签: php symfony symfony-2.8

我有2个字段徽标和图像,用于存储图像。在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);
            }
        }
    }

Good.orm.yml

lifecycleCallbacks:
            prePersist: [ preUpload, setCreatedAtValue]
            preUpdate: [ preUpload, setUpdatedAtValue ]

这里我在右侧文件夹中移动图像,并在分配此变量字符串值后。它运作良好。但我有一个问题。如果我更新了其他字段,并且没有上传新图像,我将在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(),
    ));
}

1 个答案:

答案 0 :(得分:0)

一个简单的解决方法是在表单的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实体。 我发现article描述了上述结构。