我使用Symfony2.5和Doctrine2.4.2。我有两个实体,PageImages,它扩展页面
页面:
/**
* @ORM\ManyToMany(targetEntity="PageImage",cascade={"persist"})
*/
private $images;使用窗体集合创建向页实体添加图像
当我为PageImage创建一个新条目时,它运行得很好,PrePersist、PreUpdate、PostPersist、PostUpdate都在运行,
,但是当我试图为现有的PageImage更改图像时,事件不会触发,也不会上传新的图像。
Update:它不会更新,因为$file字段只是一个虚拟的,并且理论没有看到新的数据,事件也不会触发,所以最简单的解决方案是:
public function setFile(UploadedFile $file = null)
{
$this->temp = $this->getPath();
$this->file = $file;
$this->preUpload();
}发布于 2014-08-12 13:50:02
我找到了一个解决办法,也许它不是很漂亮,有人可以建议任何其他方法来解决这个问题。
我已经发现了问题所在,因为$file是虚拟字段理论,认为实体中没有新的数据,不需要持久化或更新它,事件也不会触发,所以我需要自己运行setFile():
public function setFile(UploadedFile $file = null)
{
$this->temp = $this->getPath();
$this->file = $file;
$this->preUpload();
}发布于 2014-08-12 10:55:44
在编辑操作中,您必须手动利用“上载”方法到当前图像,如下所示
$currentImage->upload();
$entityManager->persist($currentImage);
$entityManager->flush();https://stackoverflow.com/questions/25262349
复制相似问题