简体   繁体   English

Doctrine要求在OneToMany单向关联中使用mappedBy

[英]Doctrine requires mappedBy in a OneToMany unidirectional association

When I try to make a OneToMany unidirectional association between this two entities i get this error when i try to update the database schema: 当我尝试在这两个实体之间建立OneToMany单向关联时,我在尝试更新数据库模式时遇到此错误:

$ app/console doctrine:schema:update --dump-sql $ app / console doctrine:schema:update --dump-sql

[Doctrine\\ORM\\Mapping\\MappingException] [学说\\ ORM \\映射\\ MappingException]
OneToMany mapping on field 'address' requires the 'mappedBy' attribute. 字段'address'上的OneToMany映射需要'mappedBy'属性。

/**
 * User
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class User
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\OneToMany(targetEntity="Address")
     * @ORM\JoinTable(name="users_address",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="address_id", referencedColumnName="id", unique=true)}
     *      )
     */
    private $address;

    //...
}

/**
 * Address
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Address
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    // ...
}

Why is "mappedBy" required if the association is unidirectional? 如果关联是单向的,为什么需要“mappedBy”? Thanks in advance. 提前致谢。

UPDATE: just as mentioned in the comment by @tchap an unidirectional OneToMany can be mapped with a @ManyToMany and a unique constraint on one of the join columns to enforce the onetomany cardinality. 更新:正如@tchap在评论中所提到的,单向OneToMany可以与@ManyToMany映射,并在其中一个连接列上使用唯一约束来强制执行onetomany基数。 Just as the documentation says, but it was a bit confusing for me because there is already a @OneToMay annotation. 正如文档所说,但对我来说有点混乱,因为已经有@OneToMay注释。 So I just have to change the above code to this (by only changing the @OneToMany to @ManyToMany): 所以我只需要将上面的代码改为此(只需将@OneToMany更改为@ManyToMany):

/**
 * @ORM\ManyToMany(targetEntity="Address")
 * @ORM\JoinTable(name="users_address",
 *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="address_id", referencedColumnName="id", unique=true)}
 * )
 */
private $address;

A OneToMany has to be bi-directional and is always the inverse side of a relationship and if you define the inverse side you need to point at the owning side of the relationship with a mappedBy attribute. OneToMany必须是双向的,并且始终是关系的反面,如果定义反向边,则需要指向与mappedBy属性的关系的拥有方。 The owning side is ManyToOne . 拥有方是ManyToOne In your case this would look like this: 在你的情况下,这将是这样的:

In User your association has a mappedBy="user" attribute and points to the owning side Address : User您的关联具有mappedBy="user"属性并指向拥有方Address

/** ONE-TO-MANY BIDIRECTIONAL, INVERSE SIDE
 * @var Collection
 * @ORM\OneToMany(targetEntity="Address", mappedBy="user")
 */
protected $addresses;

In Address your association has a inversedBy="addresses" attribute and points to the inverse side User : Address您的关联具有inversedBy="addresses"属性并指向反向User

/** MANY-TO-ONE BIDIRECTIONAL, OWNING SIDE
 * @var User
 * @ORM\ManyToOne(targetEntity="User", inversedBy="addresses")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
protected $user;

The advantage of this solution is that you can find which user owns the Address by doing: $address->getUser(); 此解决方案的优点是您可以通过执行以下操作找到哪个用户拥有该Address$address->getUser(); and that you skip adding an additional join table to your database. 并且您跳过向数据库添加其他连接表。


If you want the relationship to be uni-directional you can do as you did in your update; 如果您希望关系是单向的,您可以像在更新中那样做; define a ManyToMany relationship with a join table and add a unique constraint on the address_id column. 使用连接表定义ManyToMany关系,并在address_id列上添加唯一约束。

/**
 * @ORM\ManyToMany(targetEntity="Address")
 * @ORM\JoinTable(name="user_address",
 *     joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *     inverseJoinColumns={@ORM\JoinColumn(name="address_id", referencedColumnName="id", unique=true)}
 * )
 */

The disadvantage of this solution is that you cannot find out which User owns the address from the Address resource (the Address is not aware of the User ). 此解决方案的缺点是您无法找到哪个User拥有Address资源中的AddressAddress不知道User )。 Such example can also be found here in the Doctrine documentation chapter 5.6. 这样的例子也可以在Doctrine文档第5.6章中找到 One-To-Many, Unidirectional with Join Table . 具有连接表的一对多,单向

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

相关问题 Doctrine2 OneToMany没有mappedBy - Doctrine2 OneToMany without mappedBy 如何删除与教义2的单向多对多关联 - how to remove a unidirectional many to many association with Doctrine 2 双向或单向ManyToOne准则2关联映射 - bidirectional or unidirectional ManyToOne doctrine2 Association Mapping 将ManyToMany关联分为2对OneToMany / ManyToOne(原则) - Splitting a ManyToMany association into 2 pairs of OneToMany/ManyToOne (Doctrine) 主义关联一对一映射不起作用 - Doctrine Association Mapping OneToMany Bidirectional do not work 与OneToMany关联持久保存教义实体时出错 - Error when persisting doctrine entity with OneToMany association 在Doctrine2中的多对多,单向,自引用关联 - Many-to-Many, Unidirectional, Self-referencing association in Doctrine2 如何定义具有多个mappedBy列的OneToMany关系,并且其中之一在准则2和symfony 4中具有固定值? - How to define a OneToMany relation with multiple mappedBy columns and one of them has fixed value in doctrine 2 & symfony 4? Doctrine2的多对多关联和唯一一对多关系的双向和插入操作 - Doctrine2 Association for ManyToMany Bidirectional and Insert operation for Unique OneToMany Relationships oneToMany / ManyToOne双向关联未在doctrine2中生成联接列 - oneToMany/ManyToOne bidirectional association not generating join columns in doctrine2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM