I am a student, actually working on my own Symfony2 project, and after a few days I can’t find a solution to my problem.
UPDATE: 09/03/2013
I have the current version of symfony and the sonata admin package, and I need a form in my admin with multiple image downloads.
The following code that 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 a Projects entity (Pf \ Bundle \ BlogBundle \ Entity \ Projects.php) in my package. In this entity I have $ image1 (which is equivalent to the file name in the document) and, of course, the unmapped property file. All lines and are configured as it should be. (Note that I use image1 instead of the file name 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; class Projects { const SERVER_PATH_TO_IMAGE_FOLDER = '/uploads/medias'; private $file; public function setFile(UploadedFile $file = null) { $this->file = $file; } public function getFile() { return $this->file; } public function upload() {
I also have an admin controller (Pf \ Bundle \ BlogBundle \ Admin \ ProjectsAdmin.php), where I have the following form (created in the "Sonata Administration Method"):
<?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 CREATE NEW project, image1 will be invalid each time I try to upload. I can make it null in an object, but then I don't get any url at all in the database
An exception occurred while executing "INSERT INTO Projects ..." Violation of integrity constraint: 1048 Column 'image1' cannot be null
Editing an existing project in the admin seems to work .. almost ... I don't get any errors when downloading the file, BUT I get the temporary path in the database and not a single file was moved to a good folder.
It looks like the load function is not being called. I am trying to debug it but cannot find a solution.
I followed the documentation step by step. The only difference is that I am not using any .yaml file to configure my object. Should I? I use annotations on my symfony, I think it is not good to use orm.yaml and annotation at the same time ... right?
Any help is more than welcome!