php - Update entity file field -


i have issues update of entity file field on it. don't know can be, because did lot of time, today won't work. entity:

    <?php  namespace appbundle\entity;  use doctrine\orm\mapping orm; use symfony\component\validator\constraints assert;  /**  * consigli  *   * @orm\table()  * @orm\haslifecyclecallbacks  *  @orm\entity(repositoryclass="appbundle\entity\repository\consiglirepository")  */ class consigli {      /**      * @var integer      *      * @orm\column(name="id", type="integer")      * @orm\id      * @orm\generatedvalue(strategy="auto")      */     private $id;      /**      * @var string      *      * @orm\column(name="titolo", type="string", length=255)      */     private $titolo;      /**      * @var string      *      * @orm\column(name="testo", type="text")      */     private $testo;       /**      * @var string $image      * @assert\file( maxsize = "60000000", mimetypesmessage = "perfavore inserisci un'immagine valida!")      * @orm\column(name="image", type="string", length=255, nullable=true)      */     private $image;      /**      * @orm\manytoone(targetentity="categoria", inversedby="categoria")      * @orm\joincolumn(name="categoria_id", referencedcolumnname="id")      */     protected $categoria;       /**      * @var date      *      * @orm\column(name="data", type="date")      */     public $data;       /**      * id      *      * @return integer      */     public function getid()     {         return $this->id;     }      /**      * set titolo      *      * @param string $titolo      *      * @return consigli      */     public function settitolo($titolo)     {         $this->titolo = $titolo;          return $this;     }      /**      * titolo      *      * @return string      */     public function gettitolo()     {         return $this->titolo;     }   /**      * set testo      *      * @param string $testo      *      * @return consigli      */     public function settesto($testo)     {         $this->testo = $testo;          return $this;     }      /**      * testo      *      * @return string      */     public function gettesto()     {         return $this->testo;     }      /**      * set image      *      * @param string $image      */     public function setimage($image)     {         $this->image = $image;     }      /**      * image      *      * @return string      */     public function getimage()     {         return $this->image;     }       public function getfullimagepath() {         return null === $this->image ? null : $this->getuploadrootdir(). $this->image;     }      protected function getuploadrootdir() {         // absolute directory path uploaded documents should saved         return $this->gettmpuploadrootdir().$this->getid()."/";     }      protected function gettmpuploadrootdir() {         // absolute directory path uploaded documents should saved         return __dir__ . '/../../../web/immaginiconsigli/';     }  /**  * @orm\prepersist()  * @orm\preupdate()  */ public function uploadimage() {     // file property can empty if field not required     if (null === $this->image) {         return;     }     if(!$this->id){         $this->image->move($this->gettmpuploadrootdir(), $this->image->getclientoriginalname());     }else{         return null; } $this->setimage($this->image->getclientoriginalname()); }      /**      * @orm\postpersist()      */     public function moveimage()     {         if (null === $this->image)         {             return;         }         if (!is_dir($this->getuploadrootdir()))         {             mkdir($this->getuploadrootdir());         }         copy($this->gettmpuploadrootdir() . $this->image, $this->getfullimagepath());         unlink($this->gettmpuploadrootdir() . $this->image);     }      /**      * set data      *      * @param \datetime $data      *      * @return consigli      */     public function setdata($data)     {         $this->data = $data;          return $this;     }      /**      * data      *      * @return \datetime      */     public function getdata()     {         return $this->data;     }      /**      * set categoria      *      * @param \appbundle\entity\categoria $categoria      *      * @return consigli      */     public function setcategoria(\appbundle\entity\categoria $categoria = null)     {         $this->categoria = $categoria;          return $this;     }      /**      * categoria      *      * @return \appbundle\entity\categoria      */     public function getcategoria()     {         return $this->categoria;     } } 

the file stored in folder, can see "immaginiconsigli", , in table of database have "image" field, stores name of image.

the update action in controller is:

public function modificaconsiglioaction(request $request, $id)     {          $em = $this->getdoctrine()->getmanager();         $consiglio = $em->getrepository('appbundle:consigli')->find($id);         $form = $this->createform(new consigliotype($consiglio), $consiglio);         $form->handlerequest($request);         if ($form->isvalid())         {             $em = $this->getdoctrine()->getmanager();             try             {                 $em->persist($consiglio);                 $em->flush();                  return $this->redirect($this->generateurl('successconsigliomodificato'));             } catch (\exception $e)             {                  $form->adderror(new formerror('errore nel database: ' . $e->getmessage()));             }              if ($form->isvalid())             {                 $var = $consiglio;                 $em->persit($var);                 $em->flush();                  return $this->redirect($this->generateurl('successconsigliomodificato'));             } else             {              }         }         return $this->render('adminarea/modificaconsiglio.html.twig', array(                     'consiglio' => $consiglio,                     'form' => $form->createview()));     } 

so happens is: want update existing record, of course has file in it. update seems work, when render new , updated record, image doesn't show, , in database this:

/tmp/php1qyeyr

that not name of image. , also, new file/image, isn't inserted in folder. so, knows i'm doing wrong?

after looking again, think see problem.

your query return array of matched entities, not match itself. need call findoneby instead.

public function modificaconsiglioaction( request $request, $id ) {     $em = $this->getdoctrine()                ->getmanager();     $consiglio = $em->getrepository( 'appbundle:consigli' )                     ->findoneby( [ 'id' => $id ] );      if ( null !== $consiglio ) {         $form = $this->createform( new consigliotype( $consiglio ), $consiglio );         $form->handlerequest( $request );         if ( $form->issubmitted() && $form->isvalid() ) {             $em->persist( $consiglio );             $em->flush();              return $this->redirect( $this->generateurl( 'successconsigliomodificato' ) );         }     } else {          // letting user know id matched nothing     }      return $this->render( 'adminarea/modificaconsiglio.html.twig', [         'consiglio' => $consiglio,         'form'      => $form->createview(),     ] ); } 

its worth noting can away without making own query database. if change method signature to:

public function modificaconsiglioaction( request $request, consigli $consigli ) { 

symfony automatically query database based on $id sent controller in route.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -