一对多无连接表原则
我正在使用 Doctrine,我想知道是否可以在不使用连接表的情况下建立一对多关系?
I am using Doctrine and I am wondering if I could have a One To Many relationship without the use of a join table ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
取决于您希望它是单向的还是双向的。
单向一对多只能通过连接表来实现,出于“意识形态”原因,它来自Java的hibernate:
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-unidirect-with-join-table< /a>
假设您有一个包含许多产品的类别,并且您想要一种单向关系 - 即类别了解许多产品,但产品不了解类别。
如果您将“category_id”字段放入产品表中,您将使产品表“意识到”它连接到类别这一事实。如果不更改产品表,您将无法删除此关系 - 这意味着它不是真正的单向。
但是,如果您将类别和产品之间的关系保留在单独的联接表中,则可以将其删除,而产品甚至不会注意到某些内容发生了变化。
Depends on whether you want it to be unidirectional or bidirectional.
Unidirectional one-to-many can be made only through join table, for "ideological" reasons, which came from Java's hibernate:
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-unidirectional-with-join-table
Imagine you have a Category which has many Products, and you want a unidirectional relationship - i.e. category knows about many products, but products don't know about category.
If you put a 'category_id' field into Product table, you will make product table "aware" of the fact that it is connected to category. And you will not be able to remove this relationship without altering Product table - that means it is not truly unidirectional.
But if you keep relationship between category and products in the separate join table, you can just drop it and Product won't even notice that something has changed.
仅 m:n 关系才需要连接表。
A join table is only required for m:n relations.