Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle File Uploads with Doctrine in Symfony2
    text
    copied!<p>I am trying to upload image files with Doctrine in Symfony2 but I am getting the following error.: <strong>SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null</strong>. Here is my Entity class</p> <pre><code>&lt;?php // src/Acme/DemoBundle/Entity/Document.php namespace Acme\DemoBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** * @ORM\Entity * @ORM\HasLifecycleCallbacks * @ORM\Table(name="document") */ class Document { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ public $id; /** * @ORM\Column(type="string", length=255) * @Assert\NotBlank */ public $name; /** * @ORM\Column(type="string", length=255, nullable=true) */ public $path; /** * @Assert\File(maxSize="6000000") */ public $file; public function getUploadRootDir() { return '/uploads'; } /** * Get id * * @return integer $id */ public function getId() { return $this-&gt;id; } /** * Set name * * @param string $name */ public function setName($name = 'akshaya') { $this-&gt;name = $name; } /** * Get name * * @return string $name */ public function getName() { return $this-&gt;name; } /** * Set path * * @param string $path */ public function setPath($path) { $this-&gt;path = $path; } /** * Get path * * @return string $path */ public function getPath() { return $this-&gt;path; } /** * @ORM\PrePersist() */ public function preUpload() { if ($this-&gt;file) { $this-&gt;setPath($this-&gt;file-&gt;guessExtension()); } } /** * @ORM\PostPersist() */ public function upload() { if (!$this-&gt;file) { return; } try{ $this-&gt;file-&gt;move($this-&gt;getUploadRootDir(), $this-&gt;id.'.'.$this-&gt;file-&gt;guessExtension()); } catch(FilePermissionException $e) { return false; } catch(\Exception $e) { throw new \Exception($e-&gt;getMessage()); } unset($this-&gt;file); } /** * @ORM\PreRemove() */ public function removeUpload() { if ($file = $this-&gt;getFullPath()) { unlink($file); } } public function getFullPath() { return null === $this-&gt;path ? null : $this-&gt;getUploadRootDir().'/'.$this-&gt;id.'.'.$this-&gt;path; } } </code></pre> <p>Here is the related Controller class</p> <pre><code>&lt;?php namespace Acme\DemoBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\RedirectResponse; use Acme\DemoBundle\Form\ContactForm; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Imagine\Gd\Imagine; use Imagine\Image\Box; use Acme\DemoBundle\Entity\Document; class FileUploadController extends Controller { protected $file; protected $document; public function indexAction() { $this-&gt;document = new Document(); $form = $this-&gt;get('form.factory') -&gt;createBuilder('form') -&gt;add('name','text') -&gt;add('file','file') -&gt;getForm(); $request = $this-&gt;get('request'); if ($request-&gt;getMethod() === 'POST') { $form-&gt;bindRequest($request); if ($form-&gt;isValid()) { $em = $this-&gt;get('doctrine.orm.entity_manager'); $this-&gt;document-&gt;upload(); $em-&gt;persist($this-&gt;document); $em-&gt;flush(); $this-&gt;get('session')-&gt;setFlash('notice', 'The file is uploaded!'); } } return $this-&gt;render('AcmeDemoBundle:FileUpload:index.html.twig', array("form"=&gt;$form-&gt;createView())); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload