0

我有以下实体在我的数据库:学说2 - 如何删除一个相关实体并将其设为空?

class Product 
{ 
    // ... 
    /** 
    * @OneToMany(targetEntity="Feature", mappedBy="product") 
    **/ 
    private $features; 
    // ... 
} 

class Feature 
{ 
    // ... 
    /** 
    * @ManyToOne(targetEntity="Product", inversedBy="features") 
    * @JoinColumn(name="product_id", referencedColumnName="id") 
    **/ 
    private $product; 
    // ... 
} 

在我的数据库,我有一个产品的实体和与之相关的许多功能。这是一个例子,但由于某些原因,我需要删除Product实体,并同时在分配给已删除对象的features实体中设置NULL字段“product_id”。

可以这样做只呼叫$this->getDoctrine()->getManager()->remove($product)

回答

4

编辑您的实体映射:

class Feature 
{ 
    /** 
    * @ManyToOne(targetEntity="Product", inversedBy="features") 
    * @JoinColumn(name="product_id", referencedColumnName="id", onDelete="set null") 
    **/ 
    private $product; 
} 
现在

,更新架构

+0

我更喜欢用大写字母** onDelete = “SET NULL” ** –