了解原则级联操作 [英] Understanding Doctrine Cascade Operations

查看:91
本文介绍了了解原则级联操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查我对理论协会对级联操作的理解。为了这个问题,我有两个模型:客户 Insuree



如果我定义了一个客户 Insuree 之间的许多关系,并设置 cascade {all} ,据了解,这将:




  • 添加新的保险公司一个客户将坚持这个保险公司,并在连接表中创建一个协会。

  • 从集合中删除一名保险公司将把保险人与客户分离,并将客户从保险公司中分离出来。 li>
  • 删除客户将删除与客户相关联的所有保险。



这是客户

  / ** 
* @ ORM\ManyToMany(targetEntity =Insuree,inversedBy =customers,cascade = {all})
* @ ORM\JoinTable(name =customer_insuree,
* joinColumns = { @ ORM\JoinColumn(name =customer_id,引用ColumnName =id)},
* inverseJoinColumns = {@ ORM\JoinColumn(name =insuree_id,referencedColumnName =id)}
*)
* /
保护$保险;

如果我定义了一个 Insuree 客户并设置 cascade {all} ,我明白这将:




  • 向保险公司添加新客户将持续该客户并在连接表中创建一个关联。

  • 从客户处卸下客户将把客户与保险公司分离,并将保险人与客户分开。

  • 删除保险人将删除与之相关的所有客户。



这是 Insurees 之间的关联定义。

  / ** 
* @ ORM\ManyToMany(targetEntity =Customer,mappedBy =insurees,cascade = {all})
* /
保护$客户;

如果我然后将关系定义为级联在持久性上,合并和分离 - 删除保险丝将不会删除所有相关客户 - 它只会删除保险公司与其客户之间的关联?

  / ** 
* @ ORM\ManyToMany(targetEntity =Customer,mappedBy =insurees,cascade = {persist,merge,detach})
* /
protected $ customers;


解决方案

删除



你是正确的 cascade = {persist} 教条也将持续收集中的所有B个实体。



您还对 cascade = {remove}
但是我怀疑你会想要在ManyToMany关联上使用它,因为当你删除实体A级联这个操作对所有B实体,这些B实体可能与其他A实体相关联。



detach&合并



正确的 cascade = {detach} cascade = {merge}



从集合中添加/删除实体是一种需要做(在你的代码中)。请阅读此处



分离意味着您从EntityManager中分离实体。 EntityManager将不再管理该实体。这使得一个分离的实体与新实例化的实体相同,只不过它已经在数据库中(但是使EntityManager不知道)。



换句话说: c $ c> cascade = {detach} 意味着拆除实体A,Doctrine也将分离集合中的所有B实体。



合并分离相反:您将把分离的实体合并到EntityManager中。

请注意 merge()将实际返回一个新的管理对象,您传递给它的分离对象仍然不受管理。


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.

If I define a many to many relationship between a Customer and Insuree and set cascade{"all"}, I understand that this will:

  • 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.

/**
 * @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:

  • 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.

/**
 * @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;

解决方案

persist & remove

You are correct about cascade={"persist"} meaning that persisting entity A, Doctrine will also persist all B entities in the Collection.

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.

detach & merge

You are not correct about cascade={"detach"} and cascade={"merge"}:

Adding/removing entities from the Collection is something you need to do (in your code). Read about that here.

Detach means 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).

In other words: cascade={"detach"} means that detaching entity A, Doctrine will also detach all B entities in the Collection.

Merge is the opposite of detach: You will merge a detached entity back into the EntityManager.
Do note that merge() will actually return a new managed object, the detached object you passed to it remains unmanaged.

这篇关于了解原则级联操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆