1

I'm trying to resize an image after persisting an entity with Doctrine. In my Entity code, I'm setting a field to a specific value before the flush and the update :

 /**
 * @ORM\PrePersist()
 * @ORM\PreUpdate()
 */
public function preUpload()
{
    if (null !== $this->getFile()) {
        // do whatever you want to generate a unique name
        $filename = sha1(uniqid(mt_rand(), true));
        $this->image = $filename.'.png';
    }
}

So the image field is supposed to be updated. Then in my controller, I'd like to do my resize job:

if ($form->isValid()) 
    {
        $em->persist($activite);
        $em->flush();

        //resize the image
        $img_path = $activite->getImage();
        resizeImage($img_path);
    }

However, at this point in the code, the value of $activite->image is still null. How can I get the new value?

(Everything is saved well in the database.)

4

2 回答 2

3

EntityManager有一种 方法可以使用refresh()数据库中的最新值更新您的实体。

$em->refresh($entity);
于 2013-09-04T16:37:55.810 回答
0

我发现了我的错误。

实际上,我正在关注本教程:http ://symfony.com/doc/current/cookbook/doctrine/file_uploads.html

并且在某些时候他们给出了这个代码来设置文件:

 public function setFile(UploadedFile $file = null)
{
    $this->file = $file;
    // check if we have an old image path
    if (isset($this->path)) {
        // store the old name to delete after the update
        $this->temp = $this->path;
        $this->path = null;
    } else {
        $this->path = 'initial';
    }
}

然后在上传后,在第一个版本中(使用随机文件名),他们这样做:

$this->file = null;

但是在第二个版本中,这段代码被替换为:

$this->setFile(null);

我的问题是我已经尝试了这两个版本,最终还是回到了第一个版本。但是,我忘记更改将文件设置为空的行,因此每次我的路径字段都重置为空。

对这种荒谬感到抱歉,并感谢您的帮助。

于 2013-09-04T16:44:50.257 回答