如何在生命周期中阻止学说更新我的实体

时间:2015-08-31 05:16:36

标签: php symfony doctrine-orm doctrine

我正在Symfony 2中的一个实体上工作。如果我上传一个zip文件,我的实体打开zip文件,找到一个名为_projet的文件生成一个名字。问题是,如果发生错误,我需要停止整个上传和更新过程,例如它无法打开zip或者它找不到名为_projet的文件。我基本上会调整本食谱中的内容:

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

有人说:

// if there is an error when moving the file, an exception will
// be automatically thrown by move(). This will properly prevent
// the entity from being persisted to the database on error

所以我想做同样的事情,如果发生了一些不好的事情,并且可能以某种方式告诉我的控制器存在问题(我在控制器中使用flush())

到目前为止,这是我的代码:

/**
 * @ORM\PrePersist()
 * @ORM\PreUpdate()
 */
public function preUpload()
{
    //If there is a file for the project
    if (null !== $this->getFichierFile())
    {
        //Create uniq name
        $nomFichierProjet = sha1(uniqid(mt_rand(), true));

        //Check if it's a zip
        if($this->getFichierFile()->guessExtension()=="zip")
        {
            //New zip archive
            $zip=new ZipArchive;

            //While indicate if an error happen
            $erreur=true;

            //Open the zip
            if($zip->open($formulaire['fichierFile']->getData())===TRUE)
            {
                //Loop throw file in zip
                for($i=0; $i<$zip->numFiles; $i++) 
                {
                    //If the file is named _projet
                    if(pathinfo($zip->getNameIndex($i))['filename']=="_projet")
                    {
                        //Crate the file name
                        $this->fichier=$nomFichierProjet.'.'.pathinfo($zip->getNameIndex($i))['extension'];

                        //No error
                        $erreur=false;

                        //Quit loop
                        break;
                    }
                }
            }

            //If an error happen
            if($erreur)
            {
                //Prevent persisting
            }

        }
        else
        {
            //Create file name
            $this->fichier = $nomFichierProjet.'.'.$this->getFichierFile()->guessExtension();
        }
    }
}

2 个答案:

答案 0 :(得分:3)

您可以这样做,定义自定义上传例外。如果在处理上载期间发生错误并将其捕获到控制器中并处理错误,则应抛出它:

因此,通过扩展基本异常

来创建异常
<?php
// path/to/your/AppBundle/Exception
namespace AppBundle\Exception;

class UploadException extends \Exception{
    // Nothing special here, but you also can collect
    // more information if you want, by overriding the 
    // constructor, or use public properties or getter
    // and setter. it all up to you ;)
}
如果在处理上传过程中出现错误,请将

投入您的实体

//in your Entity::preUpload() method
// [...]

//If an error happen
if($erreur)
{
    // Prevent persisting
    // It does not matter, which exception is thrown here, you could even
    // throw a "normal" `\Exception`, but for better error handling you
    // should use your own.
    throw new \AppBundle\Exception\UploadException('An error occourred during upload!');
}

并在你的控制器中捕获并处理它:

// in your controller
try{
    $em->persist($entity);
    $em->flush();
} catch (\AppBundle\Exception\UploadException $e){
    // Handle the upload error
    // Catching your own exception (`\AppBundle\Exception\UploadException`)
    // you are sure now that the error occurred during your upload handling.
}

快乐编码

答案 1 :(得分:0)

Flush的调用是唯一能为您的实体提供更新的东西,所以我很常见,就像skroczek一样。如果您不想触发异常,可以使用以下数组执行此操作:

$errors = array();
//zip not found
errors[] = "zip not found";
if (count($errors) == 0) {
  $em->persist($entity);
  $em->flush();
}

这对你有帮助吗?