Symfony,持久化具有跨架构关系的实体

时间:2017-03-15 20:01:47

标签: php symfony doctrine-orm symfony-3.2

当尝试在另一个数据库模式中持久化具有关系表的实体时,实体管理器会抛出异常:

A new entity was found through the relationship 'CoreBundle\Entity\User#role' that was not configured to cascade persist operations for entity: CoreBundle\Entity\Role@000000000e0fa4f40000000012a34a59. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist  this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'CoreBundle\Entity\Role#__toString()' to get a clue.

我已经完成了一些测试,根据我读过的内容,发生这种情况的原因是因为 new 实体的EntityManager并不知道来自其他模式的相关实体是MANAGED,所以它也试图将它保存到数据库中。

所以我的问题是:如何告诉EntityManger A(新实体),由EntityManager B管理的实体(旧实体,通过不同的模式相关)不需要保留?

对于旧实体,EntityManager A将实体状态记录为DETACHED,EntityManager B将其记录为MANAGED。

0 个答案:

没有答案