SonataAdminBundle File Download: Error

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; /** * 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('Ymd 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 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!

0
upload image symfony doctrine2
source share
1 answer

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

You must insert the image form into the parent form. For example,

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

Instead of putting multiple images1, image2, ... create another form class, for example. MyImageType () and add it as a collection type to an existing form.

Work in that direction, good luck.

0
source share

All Articles