简体   繁体   English

原则中的多个ManyToOne关系

[英]Multiple ManyToOne relationships in Doctrine

A simple issue, but I cant find any documentation for it. 一个简单的问题,但是我找不到任何文档。

I want to join one table to two other tables, on the same column with the same type of relationship. 我想将一个表与其他两个表连接在一起,并在同一列上使用相同类型的关系。 Pretty simple: 很简单:

I have an Address table and a User table. 我有一个地址表和一个用户表。 Quite straight forward, one user can have many addresses: 直截了当,一个用户可以有多个地址:

User.php User.php

/**
 * @OneToMany(targetEntity="Address", mappedBy="user", cascade={"persist", "remove"}) 
 */
private $addresses;

Address.php 地址.php

/**
 * @ManyToOne(targetEntity="User", inversedBy="addresses") 
 */
private $user;

Now I want to add a new table which will also use Address (One supplier may have many addresses also). 现在,我想添加一个也将使用“地址”的新表(一个供应商可能也有许多地址)。

Supplier.php Supplier.php

/**
 * @OneToMany(targetEntity="Address", mappedBy="**???**", cascade={"persist", "remove"}) 
 */
private $addresses;

Obviously I cant map by User, as that points from Address to User. 显然,我无法按用户映射,因为这是从地址指向用户。 I suppose I could add in another foreign key in the address table, but Im wondering if there is a better way to do this and continue using the same foreign key column for both User and Address. 我想我可以在地址表中添加另一个外键,但是我想知道是否有更好的方法来做到这一点,并继续对User和Address使用相同的外键列。

You only need to specify a mappedBy attribute when there is a bidirectional relationship to determine which side is the owning side. 仅在存在双向关系时才需要指定一个mappedBy属性,以确定哪一方是拥有方。

From the docs: 从文档:

http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html#owning-side-and-inverse-side http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html#owning-side-and-inverse-side

  • A bidirectional relationship has both an owning side and an inverse side. 双向关系既具有拥有侧又具有相反侧。
  • A unidirectional relationship only has an owning side. 单向关系仅具有拥有方。
  • The owning side of a relationship determines the updates to the relationship in the database. 关系的拥有方确定数据库中对该关系的更新。

... ...

The following rules apply to bidirectional associations: 以下规则适用于双向关联:

The inverse side of a bidirectional relationship must refer to its owning side by use of the mappedBy attribute of the OneToOne, OneToMany, or ManyToMany mapping declaration. 双向关系的反面必须使用OneToOne,OneToMany或ManyToMany映射声明的mapledBy属性引用其所属的面。 The mappedBy attribute designates the field in the entity that is the owner of the relationship. mapledBy属性指定实体中作为关系所有者的字段。

In your case you only have unidirectional relationship which means you don't have to specify mappedBy , you may be able to leave it out. 在您的情况下,您仅具有单向关系,这意味着您不必指定mappedBy ,则可以将其省略。


Or you can add supplier to the Address class: 或者,您可以将供应商添加到Address类:

/**
 * @ManyToOne(targetEntity="Supplier", inversedBy="addresses") 
 */
private $supplier;

which then allows you to set mappedBy="supplier" in Supplier.php 然后,您可以在Supplier.php设置mappedBy="supplier"

/**
 * @OneToMany(targetEntity="Address", mappedBy="supplier", cascade={"persist", "remove"}) 
 */
private $addresses;

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

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