问题描述
两个实体 GalleryAlbum 和 GalleryImage 具有OneToMany / ManyToOne关系:
Two Entities GalleryAlbum and GalleryImage have OneToMany/ManyToOne relationship:
GalleryAlbum ====可以有====>许多GalleryImage
许多GalleryImage ===可以在===>一个GalleryAlbum
(来源如下)
有什么问题?
-
将(上传)文件添加到GalleryAlbum
Adding (uploading) files to GalleryAlbum
$ em-> persist($ album)
$em->persist($album)
$ em-> flush()
$em->flush()
对于每个上传的文件,GalleryAlbum类创建并向$ images添加一个新的GalleryImage实体
For each uploaded file GalleryAlbum class creates and adds to $images a new GalleryImage entity
我的ECHO / EXIT测试未显示GalleryImage的prePersist / preUpdate事件回调函数名为preUpload不被触发!)
My ECHO/EXIT test is not shown (GalleryImage's prePersist/preUpdate event callback function named preUpload is not triggered!)
我的新图像未保存到数据库中?为什么?
My new images are not saved to the database? Why?
奇怪的是什么! >
-
添加(上传)文件
Adding (uploading) files
$ em-> persist($ album)
$em->persist($album)
$ em-> flush()
$em->flush()
再次$ em-> flush()
我的ECHO / EXIT测试显示(GalleryImage的prePersist / preUpdate事件回调函数名为preUpload被触发!)
My ECHO/EXIT test is shown (GalleryImage's prePersist/preUpdate event callback function named preUpload is triggered!)
(如果我删除回声/退出)我的新GalleryImages现在保存!!!
(if i delete echo/exit) My new GalleryImages are saved now!!!
为什么?
为什么在我刷新时, )一次,当我刷新()两次时触发?
Why is preUpload never triggered when I flush() once, and is triggered when I flush() twice?
#src GalleryAlbum.php
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
* @ORM\Table(name="gallery_album")
*/
class GalleryAlbum
{
// some properties like id, name, description, etc
/**
* @ORM\OneToMany(targetEntity="GalleryImage", mappedBy="parent")
*/
protected $images;
/* Files container. Used for upload service. Must not be persisted. */
protected $files;
/* @ORM\Column(type="boolean", nullable=TRUE)
*
* if set to true will updateing object and calling preUpdate event callback
* becouse it's always set to null in database by prePersist event callback */
protected $files_added;
/**
* Set container files
*
* @return GalleryAlbum
*/
public function setFiles($files)
{
$this->files = $files;
$this->files_added = true;
/* setting files_added to true forces EntityManager to update
* this GalleryAlbum even if no other properties have changed */
return $this;
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if(null !== $this->files) {
foreach($this->files as $key => $file) {
$this->addGalleryElement($file);
unset($this->files[$key]);
}
}
/* Resetting property files_added to NULL
* so it always stays null in database */
$this->files_added = null;
}
/**
* Constructing new GalleryImage and setting it's file and parent
*/
public function addGalleryElement($file)
{
$element = new GalleryImage($this, $file);
$this->addGalleryImage($element);
}
}
#src GalleryImage.php
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
* @ORM\Table(name="gallery_image")
*/
class GalleryImage
{
// some properties like id, name, description, etc
/**
* @ORM\ManyToOne(targetEntity="GalleryAlbum", inversedBy="images")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
*/
protected $parent;
/* Constructing new GalleryImage */
public function __construct($parent = null, $file = null)
{
if($parent) $this->setParent($parent);
if($file) $this->setFile($file);
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
echo 'TEST: is this event callback function fired?'; exit;
if(null !== $this->file) {
$this->path = $this->file->guessExtension();
}
$this->file_added = null;
}
}
推荐答案
第一次你坚持原则会只保存$相册而不是其图像。您必须指定您希望教义通过在$ images声明中指定它来级联该持久性:
The first time you call persist doctrine will only save $album and not its images. You must specify that you want doctrine to cascade the persist by specifying it in the $images declaration:
/**
* @ORM\OneToMany(targetEntity="GalleryImage", mappedBy="parent", cascade={"persist", "remove"})
*/
protected $images;
这样当你调用persist($ album)时,它也会持续你的图像,并应该触发preUpload您的GalleryImage。有几个不同的选项可用于级联,他们在这里解释很好:
This way when you call persist($album) it will also persist your images and should trigger preUpload in your GalleryImage. There are a few different options available for cascading and they are explained well here:
这篇关于Symfony2:PrePersist / PreUpdate生命周期事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!