Doctrine ManyToMany: removing an object

后端 未结 1 547
梦如初夏
梦如初夏 2021-02-08 17:11

I have got problem with unidirectional ManyToMany relationship in Doctrine. The case is very easy: Product has many Tags. Tag can be attached to Product but also to any \"taggab

1条回答
  •  死守一世寂寞
    2021-02-08 17:51

    Ok, I managed myself by digging in Doctrine2 docs ;) Solution is to add onDelete="cascade" to @JoinColumn.

    /**
     * @Entity 
     * @Table(name="products")
     **/
    class Product {
    
        /** some other fileds here */
    
        /**
         * @ManyToMany(targetEntity="Tag")
         * @JoinTable(name="products_tags",
         *      joinColumns={@JoinColumn(name="product_id", referencedColumnName="id", onDelete="cascade")},
         *      inverseJoinColumns={@JoinColumn(name="tag_id", referencedColumnName="id", onDelete="cascade")}     
         *      )
         */
        protected $tags;
    
    }
    

    Note that, cascade={"all"} is managed on object level (in your app), while onDelete="cascade" is on database level.

    0 讨论(0)
提交回复
热议问题