php - Doctrine 合并实体与单向 OneToMany 不清除数据库条目
问题描述
首先,我使用 Doctrine v2.6.2 和 Symfony v4.1.7。
我有一个实体Product
(除其他外)与另一个实体具有单向的一对多关系AlternativeDuration
。按照 Doctrine 的文档,我的产品类中的映射如下所示:
/**
* @ORM\ManyToMany(
* targetEntity="AlternativeDuration",
* cascade={"persist", "merge", "remove"},
* orphanRemoval=true
* )
* @ORM\JoinTable(name="ProductAlternativeDurations",
* joinColumns={@ORM\JoinColumn(name="product_id", referencedColumnName="id", onDelete="CASCADE", nullable=false)},
* inverseJoinColumns={@ORM\JoinColumn(name="alternative_duration_id", referencedColumnName="id", unique=true, onDelete="CASCADE", nullable=false)}
* )
*/
protected $alternativeDurations;
我的应用程序最近开始使用 React,这意味着我现在提交了我的产品的 JSON 表示(以及一系列替代持续时间),我需要将其反序列化到Product
后端的实体中。我为此使用具有默认配置的 JMS 序列化程序。
现在我在编辑现有产品时遇到了问题,该产品已经有一个我删除的替代持续时间。提交的 JSON 如下所示:
{
"id": 1, # the ID of the existing product here
"alternativeDurations": [] # empty array because the entry is removed
}
在后端,我成功反序列化了 JSON 字符串:
$serializedProduct = $this->serializer->deserialize($jsonString, Product::class, 'json');
我在这里验证了$serializedProduct
没有替代的持续时间。然后我跟着合并+刷新。我希望合并能够获取现有产品并用$serializedProduct
.
$em->merge($serializedProduct); # $em being the EntityManager.
$em->flush();
现在我希望该AlternativeDuration
条目以及ProductAlternativeDurations
连接表条目被删除。ProductAlternativeDurations
但是,结果是删除了in 的条目,但AlternativeDuration
仍然存在。
我现在很茫然,任何人都可以指点为什么该AlternativeDuration
条目没有被删除?
编辑 19-11-2018:
看来这是 Doctrine 中的一个已知错误:#2542
也merge
将在 Doctrine3 中删除,所以我可能需要重新考虑这种方法。
解决方案
推荐阅读
- amazon-web-services - 如何在terraform中传递不同值的列表
- angular - Angular:从 localStorage 中提取值并显示值
- asp.net-core - 使用 Entity Framework Core 在 ASP.NET Core MVC 中搭建复杂数据模型
- php - 你能帮我解决这个问题吗?警告:sprintf():第 2668 行 /home/kupinmvl/public_html/wp-includes/comment-template.php 中的参数太少
- c++ - find_path() 的 CMake 问题
- postgresql - AWS RDS 只读副本 - VPC 对等互连
- c - 使用C的停车费
- ios - 评估 Error 是否为 NSError 总是成功,同时将 Error 转换为 NSError 会导致编译器错误
- python - 创建一个以函数为条件的 Pandas 数据框列
- spring-boot - SpringBoot 中的 EqualsVerifier