doctrine2 - Symfony 2 - Entity is not updated -


i working form aimed @ uploading file , updating database in symfony2. want manually set value of book_id field , not allow user change in form. in controller before using doctrine persist document calling:

$documents->setbookid('1'); 

unluckilly error indicates doctrine not recognise above hard coded value input.

an exception occurred while executing 'insert documents (book_id, marker, document_date, link, notes) values (?, ?, ?, ?, ?)' params [null, "fdd", "2015-04-04", null, "test"]: 

to mind may connected fact book_id field related books. therefore should use setbook function instead. please advice how properly?

my controler file looks this:

/** * code aimed @ checking if book chosen , therefore whether further works may carried out */ $session = new session(); if(!$session->get("app_books_chosen_lp")) return new redirectresponse($this->generateurl('app_listbooks')); // authorization goes here   $documents = new documents(); $form = $this->createform(new documentstype(), $documents); $form->add('save', 'submit', array('label' => 'dodaj dokument')); $form->handlerequest($request);  if ($form->isvalid()) {     $em = $this->getdoctrine()->getmanager();     $documents->upload();     $documents->setbookid('1');     $em->persist($documents);     $em->flush();  } return $this->render('appbundle:documents:adddocuments.html.twig', array('form' => $form->createview())); 

document class:

<?php namespace appbundle\entity; use doctrine\orm\mapping orm; use symfony\component\validator\constraints assert; use symfony\component\httpfoundation\file\uploadedfile;  /**  * @orm\entity  * @orm\table(name="documents")  * @orm\haslifecyclecallbacks  */  class documents {     /**      * @orm\column(type="integer")      * @orm\id      * @orm\generatedvalue(strategy="auto")      */     protected $id;      /**      * @orm\manytoone(targetentity="books", inversedby="documents")      * @orm\joincolumn(name="book_id", referencedcolumnname="id")      */     protected $book;      /**      * @orm\column(type="integer")      */     protected $book_id;      /**      * @orm\column(type="string", length=220)      */     protected $marker;      /**      * @orm\column(type="date", length=220)      */     protected $document_date;      /**      * @orm\column(type="string", length=220)      * @assert\file(maxsize="6000000")      */      protected $link;       /**      * @orm\column(type="text")      */      protected $notes;       /**      * id      *      * @return integer       */     public function getid()     {         return $this->id;     }      /**      * set book_id      *      * @param integer $bookid      * @return documents      */     public function setbookid($bookid)     {         $this->book_id = $bookid;          return $this;     }      /**      * book_id      *      * @return integer       */     public function getbookid()     {         return $this->book_id;     }      /**      * set marker      *      * @param string $marker      * @return documents      */     public function setmarker($marker)     {         $this->marker = $marker;          return $this;     }      /**      * marker      *      * @return string       */     public function getmarker()     {         return $this->marker;     }      /**      * set document_date      *      * @param \datetime $documentdate      * @return documents      */     public function setdocumentdate($documentdate)     {         $this->document_date = $documentdate;          return $this;     }      /**      * document_date      *      * @return \datetime       */     public function getdocumentdate()     {         return $this->document_date;     }      /**      * set link      *      * @param string $link      * @return documents      */     public function setlink($link)     {         $this->link = $link;          return $this;     }      /**      * link      *      * @return string       */     public function getlink()     {         return $this->link;     }       /**      * set notes      *      * @param string $notes      * @return documents      */     public function setnotes($notes)     {         $this->notes = $notes;          return $this;     }      /**      * notes      *      * @return string       */     public function getnotes()     {         return $this->notes;     }      /**      * set book      *      * @param \appbundle\entity\books $book      * @return documents      */     public function setbook(\appbundle\entity\books $book = null)     {         $this->book = $book;          return $this;     }      /**      * book      *      * @return \appbundle\entity\books       */     public function getbook()     {         return $this->book;     }      /*     * ### file upload process ###      */      /**      * @assert\file(maxsize="6000000")      */     private $file;      /**      * sets file.      *      * @param uploadedfile $file      */     public function setfile(uploadedfile $file = null)     {         $this->file = $file;     }      /**      * file.      *      * @return uploadedfile      */     public function getfile()     {         return $this->file;     }        public function getabsolutepath()     {         return null === $this->path             ? null             : $this->getuploadrootdir().'/'.$this->path;     }      public function getwebpath()     {         return null === $this->path             ? null             : $this->getuploaddir().'/'.$this->path;     }      protected function getuploadrootdir()     {         // absolute directory path uploaded         // documents should saved         return __dir__.'/../../../../web/'.$this->getuploaddir();     }      protected function getuploaddir()     {         // rid of __dir__ doesn't screw         // when displaying uploaded doc/image in view.         return 'uploads/documents';     }      public function upload()     {         // file property can empty if field not required         if (null === $this->getfile()) {             return;         }          // use original file name here should         // sanitize @ least avoid security issues          // move takes target directory ,         // target filename move         $this->getfile()->move(             $this->getuploadrootdir(),             $this->getfile()->getclientoriginalname()         );          // set path property filename you've saved file         $this->path = $this->getfile()->getclientoriginalname();          // clean file property won't need anymore         $this->file = null;     }  } 

okay, first since you're using manytoone relation, don't need property refering book - book_id. can remove , leave book only.

then in controller have query database book , set object document.

you can this:

$bookid = 1; // following example, let's tou know book id. $book = $em->getreference('appbundle:books', $bookid);  // check if found record , set documents // looking @ entity mapping, reference book can not null, // doing check never hurts, since example. if( $book ) {     $documents->setbook($book); } 

-update-

if want directly insert bookid, purpose of having manytoone reference in entity? you're going have start using doctrine's relations , objects properly. also, cool thing getreference method getting reference entity, without having load entity database - called proxy objects.

the method entitymanager#getreference($entityname, $identifier) lets obtain reference entity identifier known, without loading entity database. useful, example, performance enhancement, when want establish association entity have identifier

you can read further here.


Comments