Symfony2:PrePersist/PreUpdate生命周期事件未触发

iol*_*leo 6 events entity symfony doctrine-orm

两个实体GalleryAlbumGalleryImage具有OneToMany/ManyToOne关系:

One GalleryAlbum ==== can have ====> Many GalleryImage

Many GalleryImage === can be in ===> One GalleryAlbum

(以下来源)

问题是什么?

  1. 将文件添加(上传)到GalleryAlbum

  2. $ EM->坚持($专辑)

  3. $ EM->齐平()

  4. 对于每个上传的文件,GalleryAlbum类会创建一个新的GalleryImage实体并将其添加到$ images

  5. 我的ECHO/EXIT测试未显示(未触发GalleryImage的名为preUpload的prePersist/preUpdate事件回调函数!)

  6. 我的新图像没有保存到数据库中?为什么?

奇怪的是什么! 如果我做:

  1. 添加(上传)文件

  2. $ EM->坚持($专辑)

  3. $ EM->齐平()

  4. 再次$ em​​-> flush()

  5. 显示我的ECHO/EXIT测试(触发了GalleryImage的名为preUpload的prePersist/preUpdate事件回调函数!)

  6. (如果我删除echo/exit)我的新GalleryImages现在已保存!

为什么?

为什么在flush()一次时从不触发preUpload,而在flush()两次时触发preUpload?

#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;
        }
    }

use*_*727 6

第一次调用persist doctrine只会保存$ album而不是它的图像.您必须通过在$ images声明中指定它来指定您希望doctrine级联持久化:

    /**
     * @ORM\OneToMany(targetEntity="GalleryImage", mappedBy="parent", cascade={"persist", "remove"})
     */
    protected $images;
Run Code Online (Sandbox Code Playgroud)

这样当你调用persist($ album)时,它也会保留你的图像,并且应该在你的GalleryImage中触发preUpload.有一些不同的选项可用于级联,这里有很好的解释:

Doctrine传递持久性级联操作