Error in file upload with sonata-admin bundle

vimuth

I'm trying to execute a file upload with sonata admin bundle.
But when I press create button in create form this error comes.

Expected argument of type "AppBundle\Entity\UploadedFile", "Symfony\Component\HttpFoundation\File\UploadedFile" given

My entity file

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * BlogPost
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class BlogPost {

    const SERVER_PATH_TO_IMAGE_FOLDER = '/uploads';

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

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

    /**
     * @var string
     *
     * @ORM\Column(name="body", type="text")
     */
    private $body;

    /**
     * @var string
     *
     * @ORM\Column(name="filename", type="text")
     */
    private $filename;

    /**
     * @var boolean
     *
     * @ORM\Column(name="draft", type="boolean")
     */
    private $draft;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId() {
        return $this->id;
    }

    /**
     * Set title
     *
     * @param string $title
     * @return BlogPost
     */
    public function setTitle($title) {
        $this->title = $title;

        return $this;
    }

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

    /**
     * Set body
     *
     * @param string $body
     * @return BlogPost
     */
    public function setBody($body) {
        $this->body = $body;

        return $this;
    }

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

    /**
     * Set draft
     *
     * @param boolean $draft
     * @return BlogPost
     */
    public function setDraft($draft) {
        $this->draft = $draft;

        return $this;
    }

    /**
     * Get draft
     *
     * @return boolean 
     */
    public function getDraft() {
        return $this->draft;
    }

    /**
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="blogPosts")
     */
    private $category;

    public function setCategory(Category $category) {
        $this->category = $category;
    }

    public function getCategory() {
        return $this->category;
    }

    /**
     * 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(
                self::SERVER_PATH_TO_IMAGE_FOLDER, $this->getFile()->getClientOriginalName()
        );

        // set the path property to the filename where you've saved the file
        $this->filename = $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(new \DateTime());
    }

// ... the rest of your class lives under here, including the generated fields
//     such as filename and updated
}

My admin file

<?php

// src/AppBundle/Admin/BlogPostAdmin.php

namespace AppBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;

class BlogPostAdmin extends Admin {

    protected function configureFormFields(FormMapper $formMapper) {
        $formMapper
                ->tab('Post')
                ->with('Content', array('class' => 'col-md-9'))
                ->add('title', 'text')
                ->add('body', 'textarea')
                ->add('file', 'file', array(
                    'required' => false
                ))
                ->end()
                ->end()
                ->tab('Publish Options')
                ->with('Meta data', array('class' => 'col-md-3'))
                ->add('category', 'sonata_type_model', array(
                    'class' => 'AppBundle\Entity\Category',
                    'property' => 'name',
                ))
                ->end()
                ->end()
        ;
    }

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

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

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

    protected function configureListFields(ListMapper $listMapper) {
        $listMapper
                ->addIdentifier('title')
                ->add('category.name')
                ->add('draft')
        ;
    }

    public function toString($object) {
        return $object instanceof BlogPost ? $object->getTitle() : 'Blog Post'; // shown in the breadcrumb on the create view
    }

    protected function configureDatagridFilters(DatagridMapper $datagridMapper) {
        $datagridMapper
                ->add('title')
                ->add('body')
                ->add('category', null, array(), 'entity', array(
                    'class' => 'AppBundle\Entity\Category',
                    'property' => 'name',
                ))
        ;
    }

}
BENARD Patrick

In your entity file, you forgot to include the specific use...

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;  // <--- HERE

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to rename file upload with sonata bundle

From Dev

How to upload image and display on edit of profile using sonata admin bundle

From Dev

sonata admin bundle symfony

From Dev

Sonata admin file uploads : Upload files apart from the admin panel

From Dev

Error uploading file using Sonata Admin and Doctirne

From Dev

CKEditor not showing with Sonata Formatter (Sonata Admin Bundle)

From Dev

Use tags in Sonata Admin Bundle

From Dev

Configure Menu in Sonata Admin Bundle

From Dev

Conflicts with sonata admin bundle and LiipFunctionalTestBundle

From Dev

Sonata Admin Bundle - String Validation

From Dev

List Images in sonata admin bundle

From Dev

Use tags in Sonata Admin Bundle

From Dev

Sonata Admin Bundle configureRoutes getPersistentParameters

From Dev

Conflicts with sonata admin bundle and LiipFunctionalTestBundle

From Dev

Symfony 2 Sonata Media bundle: saving media file image without sonata admin

From Dev

error in sonata media bundle

From Dev

Sonata Media Bundle with Sonata Admin Bundle 3.0 (or 2.4)

From Dev

Image Upload Issue with Symfony3, Doctrine 2 and Sonata Admin Bundle

From Dev

Sonata admin bundle : unable to remove relation with sonata_type_admin

From Dev

Sonata User Bundle + Admin Bundle admin redirect after login

From Dev

Sonata Media Bundle Installation error

From Dev

JSON array to table Sonata admin bundle

From Dev

Show sidebar in show view - Sonata admin bundle

From Dev

Sonata Admin Bundle custom routes using annotations

From Dev

Remove delete checkbox Sonata Admin Bundle

From Dev

Customise exported CSV content of Sonata Admin bundle

From Dev

Store Logged in user data in sonata admin bundle

From Dev

Is it possible to add a translatable association in Sonata Admin Bundle?

From Dev

Adding entity not managed by Sonata Admin Bundle

Related Related

  1. 1

    how to rename file upload with sonata bundle

  2. 2

    How to upload image and display on edit of profile using sonata admin bundle

  3. 3

    sonata admin bundle symfony

  4. 4

    Sonata admin file uploads : Upload files apart from the admin panel

  5. 5

    Error uploading file using Sonata Admin and Doctirne

  6. 6

    CKEditor not showing with Sonata Formatter (Sonata Admin Bundle)

  7. 7

    Use tags in Sonata Admin Bundle

  8. 8

    Configure Menu in Sonata Admin Bundle

  9. 9

    Conflicts with sonata admin bundle and LiipFunctionalTestBundle

  10. 10

    Sonata Admin Bundle - String Validation

  11. 11

    List Images in sonata admin bundle

  12. 12

    Use tags in Sonata Admin Bundle

  13. 13

    Sonata Admin Bundle configureRoutes getPersistentParameters

  14. 14

    Conflicts with sonata admin bundle and LiipFunctionalTestBundle

  15. 15

    Symfony 2 Sonata Media bundle: saving media file image without sonata admin

  16. 16

    error in sonata media bundle

  17. 17

    Sonata Media Bundle with Sonata Admin Bundle 3.0 (or 2.4)

  18. 18

    Image Upload Issue with Symfony3, Doctrine 2 and Sonata Admin Bundle

  19. 19

    Sonata admin bundle : unable to remove relation with sonata_type_admin

  20. 20

    Sonata User Bundle + Admin Bundle admin redirect after login

  21. 21

    Sonata Media Bundle Installation error

  22. 22

    JSON array to table Sonata admin bundle

  23. 23

    Show sidebar in show view - Sonata admin bundle

  24. 24

    Sonata Admin Bundle custom routes using annotations

  25. 25

    Remove delete checkbox Sonata Admin Bundle

  26. 26

    Customise exported CSV content of Sonata Admin bundle

  27. 27

    Store Logged in user data in sonata admin bundle

  28. 28

    Is it possible to add a translatable association in Sonata Admin Bundle?

  29. 29

    Adding entity not managed by Sonata Admin Bundle

HotTag

Archive