原则 2 - 如何删除一个相关实体并将其他实体设置为空?
Doctrine 2 - How to remove one related entity and set to null others?
我的数据库中有以下实体:
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 实体并同时将分配给已删除对象的功能实体中的字段 "product_id" 设置为 NULL。
可以只调用 $this->getDoctrine()->getManager()->remove($product)
?
编辑您的实体映射:
class Feature
{
/**
* @ManyToOne(targetEntity="Product", inversedBy="features")
* @JoinColumn(name="product_id", referencedColumnName="id", onDelete="set null")
**/
private $product;
}
现在,更新您的架构
我的数据库中有以下实体:
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 实体并同时将分配给已删除对象的功能实体中的字段 "product_id" 设置为 NULL。
可以只调用 $this->getDoctrine()->getManager()->remove($product)
?
编辑您的实体映射:
class Feature
{
/**
* @ManyToOne(targetEntity="Product", inversedBy="features")
* @JoinColumn(name="product_id", referencedColumnName="id", onDelete="set null")
**/
private $product;
}
现在,更新您的架构