what does “the entities are privately owned” means

2019-04-17 08:16发布

问题:

I have read the following in doctrine documentation that does not mean much to me: "When using the orphanRemoval=true option Doctrine makes the assumption that the entities are privately owned and will NOT be reused by other entities."

here the link where you can find the sentence in its context. doctrine offical documentation

Can someone could give me an exemple of privately own entities in a ManyToMany relationship between entities?

回答1:

Imagine you're storing some user settings in a Settings entity which is attached to a User entity via a One-To-Many or One-To-One relation, the Settings entity won't be shared amongst several users, nor will it be reused if it's referenced user gets removed, you could say that Settings is privately owned by User and would become an orphan if User gets deleted, orphanRemoval=true prevents that from happening by deleting Settings when its "mother" User entity gets removed.

Some other questions on SO that you might find interesting :

  • https://stackoverflow.com/a/27473401/4114297
  • https://stackoverflow.com/a/25519856/4114297

Concerning Many-To-Many relations, you can use orphanRemoval on such relations and Doctrine will simply remove attached entities, even if they are still attached to other entities. Note that even though this option will work on Many-To-Many relations, it is not officially documented for such relations :

  • OneToOne : http://docs.doctrine-project.org/en/latest/reference/annotations-reference.html#annref-onetoone
  • OneToMany : http://docs.doctrine-project.org/en/latest/reference/annotations-reference.html#annref-onetomany
  • ManyToMany : http://docs.doctrine-project.org/en/latest/reference/annotations-reference.html#annref-manytomany

I don't think there's any example of a privately owned entity on a Many-To-Many relation, or it wouldn't be Many-To-Many, but Doctrine just makes the assumption it is, and will proceed to remove attached entities anyway.