Monday 15 April 2013

php - Symfony2 + SonataAdmin + VichUploaderBundle - Image Upload Field -



php - Symfony2 + SonataAdmin + VichUploaderBundle - Image Upload Field -

spent lastly days trying, newcomer symfony find solution on integrating image upload field in existing entity in sonata admin. i've been googling , next guides available unfortunately cannot figure out.

setup specified in headline

symfony2 sonataadmin vichuploaderbundle

im stuck next error:

"catchable fatal error: argument 1 passed beautify\beautiesbundle\entity\beauty::setimagefile() must instance of symfony\component\httpfoundation\file\uploadedfile, instance of symfony\component\httpfoundation\file\file given, called in /users/gmf/symfony/vendor/symfony/symfony/src/symfony/component/propertyaccess/propertyaccessor.php on line 438 , defined in /users/gmf/symfony/src/beautify/beautiesbundle/entity/beauty.php line 75 "

the image upload field renders correctly , receive error after submitting.

this line 75:

/** * if manually uploading file (i.e. not using symfony form) ensure instance * of 'uploadedfile' injected setter trigger update. if * bundle's configuration parameter 'inject_on_load' set 'true' setter * must able take instance of 'file' bundle inject 1 here * during doctrine hydration. * * @param file|\symfony\component\httpfoundation\file\uploadedfile */ public function setimagefile(uploadedfile $image = null) { $this->imagefile = $image; if ($image) { // required @ to the lowest degree 1 field changes if using doctrine // otherwise event listeners won't called , file lost $this->updatedat = new \datetime('now'); } }

hers's other closely related files:

entity file:

<?php // src/beautify/beautiesbundle/entity/beauty.php namespace beautify\beautiesbundle\entity; utilize doctrine\orm\mapping orm; utilize symfony\component\httpfoundation\file\uploadedfile; utilize vich\uploaderbundle\mapping\annotation vich; /** * @orm\entity * @orm\table(name="beauties") * @orm\haslifecyclecallbacks * @vich\uploadable */ class beauty { /** * @orm\column(type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ protected $id; /** * @orm\column(type="string", length=100) */ protected $name; /** * @orm\column(type="text") */ protected $content; /** * @vich\uploadablefield(mapping="beauty_image", filenameproperty="imagename") * @var file $imagefile */ protected $imagefile; /** * @orm\column(type="string", length=255, nullable=true, name="image_name") * @var string $imagename */ protected $imagename; /** * @orm\manytoone(targetentity="category", inversedby="beauties") * @orm\joincolumn(name="category_id", referencedcolumnname="id") */ protected $category; /** * if manually uploading file (i.e. not using symfony form) ensure instance * of 'uploadedfile' injected setter trigger update. if * bundle's configuration parameter 'inject_on_load' set 'true' setter * must able take instance of 'file' bundle inject 1 here * during doctrine hydration. * * @param file|\symfony\component\httpfoundation\file\uploadedfile */ public function setimagefile(file $image = null) { $this->imagefile = $image; if ($image) { // required @ to the lowest degree 1 field changes if using doctrine // otherwise event listeners won't called , file lost $this->updatedat = new \datetime('now'); } } /** * @return file */ public function getimagefile() { homecoming $this->imagefile; } /** * @param string $imagename */ public function setimagename($imagename) { $this->imagename = $imagename; } /** * @return string */ public function getimagename() { homecoming $this->imagename; } public function __tostring() { homecoming ($this->getname()) ? : ''; } /** * id * * @return integer */ public function getid() { homecoming $this->id; } /** * set name * * @param string $name * @return beauty */ public function setname($name) { $this->name = $name; homecoming $this; } /** * name * * @return string */ public function getname() { homecoming $this->name; } /** * set content * * @param string $content * @return beauty */ public function setcontent($content) { $this->content = $content; homecoming $this; } /** * content * * @return string */ public function getcontent() { homecoming $this->content; } /** * set category * * @param \beautify\beautiesbundle\entity\category $category * @return beauty */ public function setcategory(\beautify\beautiesbundle\entity\category $category = null) { $this->category = $category; homecoming $this; } /** * category * * @return \beautify\beautiesbundle\entity\category */ public function getcategory() { homecoming $this->category; } }

#admin file

<?php // src/beautify/beautiesbundle/admin/beautyadmin.php namespace beautify\beautiesbundle\admin; utilize sonata\adminbundle\admin\admin; utilize sonata\adminbundle\datagrid\listmapper; utilize sonata\adminbundle\datagrid\datagridmapper; utilize sonata\adminbundle\form\formmapper; utilize knp\menu\iteminterface menuiteminterface; utilize beautify\categorybundle\entity\category; class beautyadmin extends admin { // fields shown on create/edit forms protected function configureformfields(formmapper $formmapper) { $formmapper ->with('general') ->add('name', 'text', array('label' => 'name')) ->add('content', 'textarea', array('label' => 'content')) ->add('imagefile', 'file', array('label' => 'image file', 'required' => false)) ->end() ->with('category') ->add('category', 'sonata_type_model', array('expanded' => true)) ->end() ; } // fields shown on filter forms protected function configuredatagridfilters(datagridmapper $datagridmapper) { $datagridmapper ->add('name') ->add('content') ->add('category') ; } // fields shown on lists protected function configurelistfields(listmapper $listmapper) { $listmapper ->addidentifier('name') ->add('content') ->add('category') ->add('_action', 'actions', array( 'actions' => array( 'show' => array(), 'edit' => array(), 'delete' => array(), ) )) ; } } **#services file** report.listener: class: beautify\beautiesbundle\entity\beauty tags: - { name: doctrine.event_listener, event: prepersist } - { name: doctrine.event_listener, event: preupdate } **#config.yml** # other blocks vich_uploader: db_driver: orm mappings: beauty_image: uri_prefix: /images/beauties upload_destination: %kernel.root_dir%/../images

any help appreciated , create me happy

you missed use statement file class: use symfony\component\httpfoundation\file\file;

php symfony2 sonata-admin symfony-sonata vichuploaderbundle

No comments:

Post a Comment