php 了解 Doctrine 级联操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24612664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Understanding Doctrine Cascade Operations
提问by Jasper N. Brouwer
I want to check my understanding of cascade operations on Doctrine associations. For the purpose of this question, I have two models: Customer
and Insuree
.
我想检查我对 Doctrine 关联级联操作的理解。出于这个问题的目的,我有两个模型:Customer
和Insuree
。
If I define a many to many relationship between a Customer
and Insuree
and set cascade{"all"}
, I understand that this will:
如果我定义了一个许多人之间一对多的关系Customer
,并Insuree
与集cascade{"all"}
,我明白,这将:
- Adding a new insuree to a customer will persist this insuree and create an association in the join table.
- Removing an insuree from the collection will detach the insuree from the customer and detach the customer from the insuree.
- Deleting the customer will delete all insurees associated with the customer.
- 向客户添加新的被保险人将保留此被保险人并在连接表中创建关联。
- 从集合中移除被保险人将使被保险人与客户分离,并将客户与被保险人分离。
- 删除客户将删除与该客户关联的所有被保险人。
This is the definition of the association on Customers
.
这是 上关联的定义Customers
。
/**
* @ORM\ManyToMany(targetEntity="Insuree", inversedBy="customers", cascade={"all"})
* @ORM\JoinTable(name="customer_insuree",
* joinColumns={@ORM\JoinColumn(name="customer_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="insuree_id", referencedColumnName="id")}
* )
*/
protected $insurees;
If I define the inverse many to many relationship between an Insuree
and Customer
and set cascade{"all"}
, I understand that this will:
如果我定义了逆许多人的之间有很多关系Insuree
,并Customer
与集cascade{"all"}
,我明白,这将:
- Adding a new customer to an insuree will persist this customer and create an association in the join table.
- Removing a customer from the collection will detach the customer from the insuree and detach the insuree from the customer.
- Deleting the insuree will delete all customers associated with it.
- 向被保险人添加新客户将保留该客户并在连接表中创建关联。
- 从集合中移除客户将使客户与被保险人分离,并将被保险人与客户分离。
- 删除被保险人将删除与其关联的所有客户。
This is the definition of the association on Insurees
.
这是 上关联的定义Insurees
。
/**
* @ORM\ManyToMany(targetEntity="Customer", mappedBy="insurees", cascade={"all"})
*/
protected $customers;
If I then define the relationship as to cascade on persist, merge and detach - deleting the insuree will not delete all associated customers - it will only remove the associations between the insuree and its customers?
如果我然后将关系定义为级联持久、合并和分离 - 删除被保险人不会删除所有关联的客户 - 它只会删除被保险人与其客户之间的关联?
/**
* @ORM\ManyToMany(targetEntity="Customer", mappedBy="insurees", cascade={"persist", "merge", "detach"})
*/
protected $customers;
回答by Jasper N. Brouwer
persist & remove
坚持和删除
You are correct about cascade={"persist"}
meaning that persisting entity A, Doctrine will also persist all B entities in the Collection.
您是正确的cascade={"persist"}
意思是持久化实体 A,Doctrine 也将持久化集合中的所有 B 实体。
You are also correct about cascade={"remove"}
meaning that removing entity A, Doctrine will also remove all B entities in the Collection.
But I doubt you would ever want to use this on a ManyToMany association, because when you remove entity A that cascades this operation to all B entities, those B entities might be associated to other A entities.
您也正确地cascade={"remove"}
表示删除实体 A,Doctrine 也将删除集合中的所有 B 实体。
但我怀疑您是否想在多对多关联上使用它,因为当您删除将这个操作级联到所有 B 实体的实体 A 时,这些 B 实体可能与其他 A 实体相关联。
detach & merge
分离与合并
You are notcorrect about cascade={"detach"}
and cascade={"merge"}
:
你对和 的看法不正确:cascade={"detach"}
cascade={"merge"}
Adding/removing entities from the Collection is something youneed to do (in your code). Read about that here.
从集合中添加/删除实体是您需要做的事情(在您的代码中)。在这里阅读。
Detachmeans that you detach an entity from the EntityManager. The EntityManager will no longer manage that entity. This makes a detached entity the same as a newly instantiated entity, except that it's already in the database (but you made the EntityManager unaware of that).
分离意味着您从 EntityManager 分离实体。EntityManager 将不再管理该实体。这使得分离的实体与新实例化的实体相同,除了它已经在数据库中(但您让 EntityManager 不知道这一点)。
In other words: cascade={"detach"}
means that detaching entity A, Doctrine will also detach all B entities in the Collection.
换句话说:cascade={"detach"}
意味着分离实体A,Doctrine也会分离集合中的所有B实体。
Mergeis the opposite of detach: You will merge a detached entity back into the EntityManager.
Do note that merge()
will actually return a newmanaged object, the detached object you passed to it remains unmanaged.
Merge与detach相反:您将分离的实体合并回 EntityManager。
请注意,merge()
这实际上会返回一个新的托管对象,您传递给它的分离对象仍然是非托管的。