Symfony2: don't update image in db, if it's empty

Viewed 258

I have 2 fields logo and images, which I use for storing images. In db they are represented as strings.

   /**
     * @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 ]

Here I move images in right folders and after assign this vars string values. It works well. But I have one problem. If I update another fields, and don't upload new image, I will have empty image fields in db. Symfony erases this fields. How can I change this behavior?

Creation form:

       /**
         * {@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');
        }

Edit action

/**
 * 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 Answers
Related