简体   繁体   English

教义一对多没有加入表

[英]Doctrine One To Many No Join Table

我正在使用Doctrine,我想知道如果不使用连接表,我是否可以拥有一对多的关系?

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: 单向一对多只能通过连接表进行,出于“意识形态”的原因,它来自Java的休眠:

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-unidirectional-with-join-table 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 - ie 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. 如果将“category_id”字段放入Product表中,您将使产品表“意识到”它已连接到类别。 And you will not be able to remove this relationship without altering Product table - that means it is not truly unidirectional. 如果不改变Product表,你将无法删除这种关系 - 这意味着它不是真正的单向。

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关系才需要连接表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM