php - How to stop doctrine from updating my entity during is lifecycle -


i'm working on 1 of entity in symfony 2. i'm trying make if upload zip file, entity open zip file, find file name _projet , generate name. thing need stop entire uploading , updating process if error happen, isn't able open zip or doesn't find file named _projet. i'm tweaking showed in cookbook:

http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html

it said @ 1 point that:

// if there error when moving file, exception // automatically thrown move(). prevent // entity being persisted database on error 

so want same thing if bad happens , maybe somehow tell controller there problem (i use flush() in controller)

here code far:

/**  * @orm\prepersist()  * @orm\preupdate()  */ public function preupload() {     //if there file project     if (null !== $this->getfichierfile())     {         //create uniq name         $nomfichierprojet = sha1(uniqid(mt_rand(), true));          //check if it's zip         if($this->getfichierfile()->guessextension()=="zip")         {             //new zip archive             $zip=new ziparchive;              //while indicate if error happen             $erreur=true;              //open zip             if($zip->open($formulaire['fichierfile']->getdata())===true)             {                 //loop throw file in zip                 for($i=0; $i<$zip->numfiles; $i++)                  {                     //if file named _projet                     if(pathinfo($zip->getnameindex($i))['filename']=="_projet")                     {                         //crate file name                         $this->fichier=$nomfichierprojet.'.'.pathinfo($zip->getnameindex($i))['extension'];                          //no error                         $erreur=false;                          //quit loop                         break;                     }                 }             }              //if error happen             if($erreur)             {                 //prevent persisting             }          }         else         {             //create file name             $this->fichier = $nomfichierprojet.'.'.$this->getfichierfile()->guessextension();         }     } } 

you can that, defining custom upload exception. should throw if error occurred during handling upload , catch in controller , handle error:

so, create exception extending base exception

<?php // path/to/your/appbundle/exception namespace appbundle\exception;  class uploadexception extends \exception{     // nothing special here, can collect     // more information if want, overriding      // constructor, or use public properties or getter     // , setter. ;) } 

throw in entity if there error during handling upload

//in entity::preupload() method // [...]  //if error happen if($erreur) {     // prevent persisting     // not matter, exception thrown here,     // throw "normal" `\exception`, better error handling     // should use own.     throw new \appbundle\exception\uploadexception('an error occourred during upload!'); } 

and catch , handle in controller:

// in controller try{     $em->persist($entity);     $em->flush(); } catch (\appbundle\exception\uploadexception $e){     // handle upload error     // catching own exception (`\appbundle\exception\uploadexception`)     // sure error occurred during upload handling. } 

happy coding


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -