inversedBy和mappedBy有什么区别? [英] What is the difference between inversedBy and mappedBy?

查看:185
本文介绍了inversedBy和mappedBy有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Zend Framework 2和Doctrine 2开发我的应用程序。



在写入注释时,我无法理解 mappedBy inversedBy



什么时候我应该使用 mappedBy



何时应该使用 inversedBy



我什么时候不用?



这是一个例子:

  / ** 
*
* @ ORM\OneToOne(targetEntity =\custMod\Entity\Person,mappedBy =customer)
* @ ORM\JoinColumn(name =personID,referencedColumnName =id)
* /
protected $ person;

/ **
*
* @ ORM\OneToOne(targetEntity =\Auth\Entity\User)
* @ ORM\ JoinColumn(name =userID,referencedColumnName =id)
* /
protected $ user;

/ **
*
* @ ORM\ManyToOne(targetEntity =\custMod\Entity\Company,inversedBy =customer)
* @ ORM\JoinColumn(name =companyID,referencedColumnName =id)
* /
protected $ company;

我做了一个快速搜索,发现以下,但我仍然困惑:




解决方案


  • mappedBy 必须在(双向)关联的反向端中指定

  • inversedBy 必须在(双向)关联的拥有方中指定



从原则文件:




  • ManyToOne始终是拥有一个双向协议的一面。

  • OneToMany始终是双向协议的反面。

  • OneToOne协议的拥有方是具有包含外键的表。



请参阅 http://docs.doctrine-project.org/en/latest/reference/unitofwork-associations.html


I am developing my application using Zend Framework 2 and Doctrine 2.

While writting annotations, I am unable to understand the difference between mappedBy and inversedBy.

When should I use mappedBy?

When should I use inversedBy?

When should I use neither?

Here is an example:

 /**
 *
 * @ORM\OneToOne(targetEntity="\custMod\Entity\Person", mappedBy="customer")
 * @ORM\JoinColumn(name="personID", referencedColumnName="id")
 */
protected $person;

/**
 *
 * @ORM\OneToOne(targetEntity="\Auth\Entity\User")
 * @ORM\JoinColumn(name="userID", referencedColumnName="id")
 */
protected $user;

/**
 *
 * @ORM\ManyToOne (targetEntity="\custMod\Entity\Company", inversedBy="customer")
 * @ORM\JoinColumn (name="companyID", referencedColumnName="id")
 */
protected $company;

I did a quick search and found the following, but I am still confused:

解决方案

  • mappedBy has to be specified on the inversed side of a (bidirectional) association
  • inversedBy has to be specified on the owning side of a (bidirectional) association

from doctrine documentation:

  • ManyToOne is always the owning side of a bidirectional assocation.
  • OneToMany is always the inverse side of a bidirectional assocation.
  • The owning side of a OneToOne assocation is the entity with the table containing the foreign key.

See http://docs.doctrine-project.org/en/latest/reference/unitofwork-associations.html

这篇关于inversedBy和mappedBy有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆