SonataAdminBundle file upload: Error

2019-06-09 14:28发布

I'm a student actually working on my own Symfony2 project and it's few days now I can't find a solution to my problem.

UPDATE: 03.09.2013

I have the current version of symfony and of the sonata admin bundle and need a form in my admin with multiple image uploads.

The following code I present is based on this installation documentation:

http://sonata-project.org/bundles/admin/master/doc/reference/recipe_file_uploads.html

In my case I have an entity Projects (Pf\Bundle\BlogBundle\Entity\Projects.php) in my bundle. In this entity I have $image1 (which is the equivalent of filename in the doc) and of course the unmapped property file. All strings and configured as it has to be. (Note that I use image1 instead of filename in my case // documentation).

<?php

namespace Pf\Bundle\BlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;


/**
 * Projects
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Pf\Bundle\BlogBundle\Entity\ProjectRepository")
  * @ORM\HasLifecycleCallbacks()
 */
class Projects
{

    const SERVER_PATH_TO_IMAGE_FOLDER = '/uploads/medias';

    /**
     * Unmapped property to handle file uploads
     */
    private $file;

    /**
     * Sets file.
     *
     * @param UploadedFile $file
     */
    public function setFile(UploadedFile $file = null)
    {
        $this->file = $file;
    }

    /**
     * Get file.
     *
     * @return UploadedFile
     */
    public function getFile()
    {
        return $this->file;
    }

    /**
     * Manages the copying of the file to the relevant place on the server
     */
    public function upload()
    {
        // the file property can be empty if the field is not required
        if (null === $this->getFile()) {
            return;
        }
        // we use the original file name here but you should
        // sanitize it at least to avoid any security issues

        // move takes the target directory and target filename as params
        $this->getFile()->move(
            Projects::SERVER_PATH_TO_IMAGE_FOLDER,
            $this->getFile()->getClientOriginalName()
        );

        // set the path property to the filename where you've saved the file
        $this->image1 = $this->getFile()->getClientOriginalName();

        // clean up the file property as you won't need it anymore
        $this->setFile(null);
    }

    /**
     * Lifecycle callback to upload the file to the server
     */
    public function lifecycleFileUpload() {
        $this->upload();
    }

    /**
     * Updates the hash value to force the preUpdate and postUpdate events to fire
     */
    public function refreshUpdated() {
        $this->setUpdated(date('Y-m-d H:i:s'));
    }

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="image1", type="string", length=100)
     */
    private $image1;

    //...

    /**
     * @var datetime
     *
     * @ORM\Column(name="updated", nullable=true)
     */
    private $updated;

     /**
     * Set updated
     *
     * @param string $updated
     * @return Projects
     */
    public function setUpdated($updated)
    {
        $this->updated = $updated;

        return $this;
    }

    /**
     * Get updated
     *
     * @return string 
     */
    public function getUpdated()
    {
        return $this->updated;
    }
}

I also have an admin controller (Pf\Bundle\BlogBundle\Admin\ProjectsAdmin.php) where I have the following form (created in the "sonata admin way"):

<?php
namespace Pf\Bundle\BlogBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Validator\ErrorElement;

use Sonata\AdminBundle\Form\FormMapper;

class ProjectsAdmin extends Admin
{
    // setup the default sort column and order
    protected $datagridValues = array(
        '_sort_order' => 'DESC',
        '_sort_by' => 'id'
    );

    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('file', 'file', array('required' => false, 'data_class' => null))
            ->add('image2',  'text')
            ->add('image3', 'text')
            ->add('link', 'text')
            ->add('download_link', 'text')
            ->add('content1', 'text')
            ->add('content2', 'text')
            ->add('title', 'text')
            ->add('thumbnail', 'text')
        ;
    }

    public function prePersist($projects) {
        $this->manageFileUpload($projects);
    }

    public function preUpdate($projects) {
        $this->manageFileUpload($projects);
    }

    private function manageFileUpload($projects) {
        if ($projects->getFile()) {
            $projects->refreshUpdated();
        }
    }

    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add('title')
        ;
    }

    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('title')
        ;
    }
}

I have few problems:

  • If I try to CREATE A NEW project, the image1 appears to be null every time I try to upload. I can make it nullable in the entity but then I don't get any url at all in the database

    An exception occurred while executing 'INSERT INTO Projects... Integrity constraint violation: 1048 Column 'image1' cannot be null

  • By editing an existing project in the admin, it seems to work..well nearly..I don't get any errors when uploading the file BUT I get a temporary path in the database and no file has been moved in the good folder.

It looks like the upload function isn't called. I try to debug it but can't find a solution.

I have followed step by step the documentation. The only difference is that I don't use any .yaml file to configure my entity.. Does I have to? I'm using annotations on my symfony, I guess it's not good to use orm.yaml and annotation in the same time...right?

Any help is more than welcome!

1条回答
我命由我不由天
2楼-- · 2019-06-09 14:59

"any infos on this topic" Have you seen http://symfony.com/doc/current/cookbook/form/form_collections.html ?

You should embed image form into the parent form. For instance,

->add('myImage','collection',array('type'=>new MyImageType()))

Instead of putting multiple image1, image2,... make another form class, eg. MyImageType() and add it as a collection type in to an existing form.

Work in that direction, good luck.

查看更多
登录 后发表回答