I joust started playing around with Doctrine ORM library, and Im learning about all associations between tables.
我开始玩Doctrine ORM库,我正在学习表之间的所有关联。
So Im stuck with differences in Unidirectional and Bidirectional relation.
所以我坚持单向和双向关系的差异。
As I get it, Unidirectional relation has primary key only on one side, and that side is owning side right? And Bidirectional relation have primary key in both tables and therefore you can have relation from both sides, and set constrains on both sides.
我得到它,单向关系只在一侧有主键,而那边是拥有右侧的?并且双向关系在两个表中都具有主键,因此您可以从两侧获得关系,并在两侧设置约束。
Now, Im reading through Doctrine documentation about relations and there you have: Unidirectional and Bidirectional associations.
现在,我正在阅读关于关系的Doctrine文档,你有:单向和双向关联。
But they produce the same SQL, and the same tables with the same primary key-s and constrains. So I dont really see any difference in those two. And both examples have primary key on one side.
但它们生成相同的SQL,并且相同的表具有相同的主键和约束。所以我真的看不出这两者有什么不同。这两个例子都有主键。
As I get it the true Bidirectional relation should have primary keys in both tables pointing to the other table right? And with given example on Doctrine documentation that is not the case. Both examples give the same result and are the same.
当我得到真正的双向关系时,两个表中的主键应该指向另一个表吗?并且在Doctrine文档的给定示例中并非如此。两个例子都给出相同的结果并且是相同的。
So what I did, is this, lets say I have User and Card Entity, and want relation to be OneToOne Bidirectional.
所以我做了,就是这个,假设我有用户和卡实体,并希望关系是OneToOne双向。
/**
* @Entity
* @Table(name="users")
*/
class User
{
/**
* @Id
* @GeneratedValue
* @Column(type="bigint")
*/
protected $id;
/**
* @OneToOne(targetEntity="Card", mappedBy="User")
* @JoinColumn(name="card_id", referencedColumnName="id")
*/
protected $card;
/**
* @Column(name="user_name", type="string")
*/
protected $userName;
/**
* @Column(name="user_pass", type="string")
*/
protected $userPass;
}
/**
* @Entity
* @Table(name="cards")
*/
class Card
{
/**
* @Id
* @GeneratedValue
* @Column(type="bigint")
*/
protected $id;
/**
* @OneToOne(targetEntity="User", inversedBy="Card")
* @JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
/**
* @Column(name="post_title", type="string")
*/
protected $cardType;
}
The difference here is I wrote @JoinColumn in both objects/entities. And in Doctrine example there is only one. Now I would get what I think is Bidirectional relation. If i look at EER diagram, I can see one line pointing from user to card, and the other from card to user.
这里的区别是我在两个对象/实体中都写了@JoinColumn。在Doctrine示例中,只有一个。现在我会得到我认为的双向关系。如果我看一下EER图,我可以看到一条线从用户指向卡,另一条线从卡指向用户。
So basicly did I get this right? Is the Doctrine documentation wrong? :D How would Bidirectional OneToOne relation look in EER diagram?
所以基本上我做对了吗? Doctrine文档是错的吗? :D如何在EER图中看到双向OneToOne关系?
Thanks!
2 个解决方案
#1
14
The only difference is in the PHP class interface, i.e. in the presence or absence of the property that points back to the owner (e.g. the $customer
property in the mentioned Doctrine example). In other words Doctrine just needs to know whether it should take care about a single property ($shipping
) or two properties ($cart
and $customer
). There is no other difference. Therefore, the SQL code is the same (because one foreign key is sufficient for representing any 1:N relationship) and there would no difference in EER diagram neither (because in EER you typically do not solve such PHP-related implementation details).
唯一的区别在于PHP类接口,即存在或不存在指向所有者的属性(例如,在所提到的Doctrine示例中的$ customer属性)。换句话说,Doctrine只需要知道它是应该关注单个属性($ shipping)还是两个属性($ cart和$ customer)。没有其他区别。因此,SQL代码是相同的(因为一个外键足以表示任何1:N关系)并且EER图也没有区别(因为在EER中,您通常不解决此类与PHP相关的实现细节)。
#2
10
Unidirectional and bidirectional have nothing to do with the background algorithm how to create these connections in the database layer.
单向和双向与后台算法无关,如何在数据库层中创建这些连接。
All they talk about is how the connections can be used. In an unidirectional relationship you can access the target only from one site. An bidirectional relationship allows the connection to be called from two (both) sides.
他们所谈论的是如何使用连接。在单向关系中,您只能从一个站点访问目标。双向关系允许从两个(两个)侧调用连接。
So in an unidir. rel. model_a can get to model_b, but model_b cant get to model_a (without extra work). If you now use a bidir. rel both models can access each other without problems
所以在unidir。相对。 model_a可以到达model_b,但是model_b无法进入model_a(没有额外的工作)。如果你现在使用bidir。两个模型可以相互访问而不会出现问题
In doctrine terms, a unidirectional relationship defines a $modelA->getModelB()
method, but not a $modelB->getModelA()
method, whereas a bidirectional relationship defines both methods (or accessors, however you want to call them)
在学说术语中,单向关系定义$ modelA-> getModelB()方法,但不定义$ modelB-> getModelA()方法,而双向关系定义两种方法(或访问者,但是要调用它们)
in an uml diagram it would look like this:
在uml图中,它看起来像这样:
unidirectional
modelA --X------> modelB
bidirectional
modelA <--------> modelB
#1
14
The only difference is in the PHP class interface, i.e. in the presence or absence of the property that points back to the owner (e.g. the $customer
property in the mentioned Doctrine example). In other words Doctrine just needs to know whether it should take care about a single property ($shipping
) or two properties ($cart
and $customer
). There is no other difference. Therefore, the SQL code is the same (because one foreign key is sufficient for representing any 1:N relationship) and there would no difference in EER diagram neither (because in EER you typically do not solve such PHP-related implementation details).
唯一的区别在于PHP类接口,即存在或不存在指向所有者的属性(例如,在所提到的Doctrine示例中的$ customer属性)。换句话说,Doctrine只需要知道它是应该关注单个属性($ shipping)还是两个属性($ cart和$ customer)。没有其他区别。因此,SQL代码是相同的(因为一个外键足以表示任何1:N关系)并且EER图也没有区别(因为在EER中,您通常不解决此类与PHP相关的实现细节)。
#2
10
Unidirectional and bidirectional have nothing to do with the background algorithm how to create these connections in the database layer.
单向和双向与后台算法无关,如何在数据库层中创建这些连接。
All they talk about is how the connections can be used. In an unidirectional relationship you can access the target only from one site. An bidirectional relationship allows the connection to be called from two (both) sides.
他们所谈论的是如何使用连接。在单向关系中,您只能从一个站点访问目标。双向关系允许从两个(两个)侧调用连接。
So in an unidir. rel. model_a can get to model_b, but model_b cant get to model_a (without extra work). If you now use a bidir. rel both models can access each other without problems
所以在unidir。相对。 model_a可以到达model_b,但是model_b无法进入model_a(没有额外的工作)。如果你现在使用bidir。两个模型可以相互访问而不会出现问题
In doctrine terms, a unidirectional relationship defines a $modelA->getModelB()
method, but not a $modelB->getModelA()
method, whereas a bidirectional relationship defines both methods (or accessors, however you want to call them)
在学说术语中,单向关系定义$ modelA-> getModelB()方法,但不定义$ modelB-> getModelA()方法,而双向关系定义两种方法(或访问者,但是要调用它们)
in an uml diagram it would look like this:
在uml图中,它看起来像这样:
unidirectional
modelA --X------> modelB
bidirectional
modelA <--------> modelB