Delete an item from oneToMany relationship

后端 未结 1 764
渐次进展
渐次进展 2020-12-15 17:02

I have the following Gallery entity

class Gallery
{
    /**
     * @var integer
     *
     * @ORM\\Column(name=\"id\", type=\"integer\")
     * @ORM\\Id
            


        
相关标签:
1条回答
  • 2020-12-15 17:28

    There is article in Symfony2 cookbook about handling this type of situation. As you have OneToMany relationship, you have to remove related objects manually in controller.

    Edit: Or you can make use of Doctrine's orphan removal feature.

    class Gallery
    {
        //...    
    
        /**
         * @ORM\OneToMany(targetEntity="Photo", mappedBy="gallery", cascade={"persist", "remove"}, orphanRemoval=true)
         */
        private $photos;
    
        //...
    
        public function removePhotos($photo)
        {
            $this->photos->remove($photo);
            $photo->setGallery(null);
        }
    }
    
    0 讨论(0)
提交回复
热议问题