简体   繁体   中英

Doctrine symfony remove entity with OneToMany - ManyToOne relationship

I have two entities Activities and BulleManager. I use OneToMany - ManyToOne for the relationship between them. The problem is that when i delete a row in BulleManager entity, the related row in the Activities entity is deleted too, and that is not the behaviour i like. I want just to set "NULL" to the inversed side entity(Activities) when the owning side(BulleManager) entity is deleted.

class Activities
{
     /**
      * @ORM\ManyToOne(targetEntity="BulleManager", 
      cascade={"persist"}, inversedBy="activities")
     * @ORM\JoinColumn(name="bulle_manager_id", 
     referencedColumnName="id", nullable=true, onDelete="SET NULL")
     */
     protected $bulleManager;
...
}

class BulleManager
{
    /**
     * @ORM\OneToMany(targetEntity="Activities", 
     mappedBy="bulleManager", cascade={"remove"})
     */
    private $activities;
}

Your solution are welcome.

Just remove cascade={"remove"} from your BulleManager entity.

cascade={"remove"} deletes all dependent records in the Child Table (Activities) if a record is deleted in Parent Table (BulleManager).

Now, it will set bullemanager_id to NULL in Activities table if a BulleManager is deleted.

And, Don't forgot to update your database schema using:

php bin/console doctrine:schema:update

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM