symfony - LifecycleCallbacks are not executed in Symfony2 -
i having problem lifecyclecallbacks
in symfony2
not being executed though have @orm\haslifecyclecallbacks
annotation. trying follow example provided in http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html#using-the-id-as-the-filename. goal save file under document id.
- the below provided code not result in error, other
the file not found.
information in reloaded page. - to check going on added
die("test");
commandupload() function
seems never executed result form page being reloaded above mentioned error.
i ask advice may reason upload() function not executed?
controller:
/** * @route("app/documents/add/", name="app_documents_add") */ public function addaction(request $request) { /** * code aimed @ checking if book choseen , 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(); $book = $em->getreference('appbundle:books', $session->get("app_books_chosen_lp")); if( $book ) $documents->setbook($book); else die ("critical error: addaction - bad book id"); $em->persist($documents); $em->flush(); } return $this->render('appbundle:documents:adddocuments.html.twig', array('form' => $form->createview())); }
documents 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\haslifecyclecallbacks * @orm\table(name="documents") */ 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="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 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; public function getwebpath() { return null === $this->link ? null : $this->getuploaddir().'/'.$this->link; } 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'; } /** * file. * * @return uploadedfile */ public function getfile() { return $this->file; } /* * temp fila path */ private $temp; /** * sets file. * * @param uploadedfile $file */ public function setfile(uploadedfile $file = null) { $this->file = $file; // check if have old image path if (is_file($this->getabsolutepath())) { // store old name delete after update $this->temp = $this->getabsolutepath(); } else { $this->link = 'initial'; } } /** * @orm\prepersist() * @orm\preupdate() */ public function preupload() { if (null !== $this->getfile()) { $this->link = $this->getfile()->guessextension(); } } /** * @orm\postpersist() * @orm\postupdate() */ public function upload() { if (null === $this->getfile()) { return; } // check if have old image if (isset($this->temp)) { // delete old image unlink($this->temp); // clear temp image path $this->temp = null; } // must throw exception here if file cannot moved // entity not persisted database // uploadedfile move() method $this->getfile()->move( $this->getuploadrootdir(), $this->id.'.'.$this->getfile()->guessextension() ); $this->setfile(null); } /** * @orm\preremove() */ public function storefilenameforremove() { $this->temp = $this->getabsolutepath(); } /** * @orm\postremove() */ public function removeupload() { if (isset($this->temp)) { unlink($this->temp); } } public function getabsolutepath() { return null === $this->link ? null : $this->getuploadrootdir().'/'.$this->id.'.'.$this->link; } } `
the answer why upload() function not executed quite easy - form marked invalid. looking why happens (the file not found while using lifecyclecallbacks)
Comments
Post a Comment