简体   繁体   English

(DOCTRINE)mappingBy和inversedBy以及targetEntity批注

[英](DOCTRINE) mappedBy and inversedBy and targetEntity annotations

What values goes inside this inversedBy annotation as well as the mappedBy annotation? 这个inversedBy批注和mappedBy批注内包含什么值? Also what is targetEntity and referencedColumnName? 还有什么是targetEntity和referencedColumnName?

Here is an example of my comment entity . 这是我的comment entity的示例。 As you can see, in my tutorials it says to write the string comments inside the inversedBy attribute and \\Application\\Entity\\Post inside the targetREntity attribute. 正如你所看到的,在我的教程,它说要写入字符串comments里面inversedBy属性和\\Application\\Entity\\Post里面targetREntity属性。

/**
 * This class represents a comment related to a blog post.
 * @ORM\Entity
 * @ORM\Table(name="comment")
 */
class Comment
{

  /**
   * @ORM\ManyToOne(targetEntity="\Application\Entity\Post", inversedBy="comments")
   * @ORM\JoinColumn(name="post_id", referencedColumnName="id")
   */
  protected $post;
}

For this one, it says comments . 对于这个,它说了comments What exactly is this comments string referring to? 此注释字符串到底指的是什么? I dont know what comments means. 我不知道评论的意思。 Is this a mapping to a table, or the ORM name of the class at the top, or something else. 这是到表的映射,还是顶部的类的ORM名称,还是其他?

Also, 也,

Here is an example of where mappedBy is used: 这是使用mappedBy的示例:

/**
 * @ORM\Entity
 * @ORM\Table(name="post")
*/
class Post 
{
  // Post status constants.
  const STATUS_DRAFT       = 1; // Draft.
  const STATUS_PUBLISHED   = 2; // Published.

  /**
   * @ORM\OneToMany(targetEntity="\Application\Entity\Comment", mappedBy="post")   
   * @ORM\JoinColumn(name="id", referencedColumnName="post_id")
   */
  protected $comments;

I started reading about owning sides and inverse sides click here but it was extremely difficult to understand. 我开始阅读有关单击此处的 owning sides and inverse sides 信息,但这很难理解。

Any details on anything here would be great. 这里的任何细节都很棒。

Any help would be great. 任何帮助都会很棒。

I'm not doctrine expect but I used to work with it for some time so I'll try to explain what I know so far. 我没有学说的期望,但是我曾经使用它一段时间,所以我将尽力解释到目前为止我所知道的。

InversedBy refers to $comments property (field) in Post entity and vice versa. InversedBy指的是Post实体中的$comments属性(字段),反之亦然。

The inverse side has to use the mappedBy attribute of the OneToOne, OneToMany, or ManyToMany mapping declaration. 反面必须使用OneToOne,OneToMany或ManyToMany映射声明的maptedBy属性。 The mappedBy attribute contains the name of the association-field on the owning side. mappingBy属性包含拥有方的关联字段的名称。

The owning side has to use the inversedBy attribute of the OneToOne, ManyToOne, or ManyToMany mapping declaration. 拥有方必须使用OneToOne,ManyToOne或ManyToMany映射声明的inversedBy属性。 The inversedBy attribute contains the name of the association-field on the inverse-side. inversedBy属性包含反侧的关联字段的名称。

When you use @ORM\\ManyToOne annotation you are creating n:1 relationship . 使用@ORM\\ManyToOne注释时,您正在创建n:1关系 There are three types of mapping : 映射有三种类型

  • bidirectional - Post with access to Comment and vice versa 双向 - Post可以访问Comment反之亦然
  • unidirectional - Post with $comments field full of Comment entities but you won't have ability to access Post from Comment , because it is not mapped back 单向 - Post$comments场十足的Comment实体,但你不会有访问能力PostComment ,因为它没有被映射回
  • self-referencing - Category with with self-reference to parent Category which is entity of same type 自参考 - Category自参考Category是同一类型的实体

TargetEntity tells to which entity you are creating relationship. TargetEntity告诉您要与哪个实体创建关系。 Imagine foreign key. 想象一下外键。 When you create foreign key you need to specify referencing table. 创建外键时,需要指定引用表。

ReferencedColumnName tells to which column foreign key should be created. ReferencedColumnName告诉应该创建哪个列外键。

Doctrine is not magic. 教义不是魔术。 It is just Object Relation Mapping. 这只是对象关系映射。 Think about it like when you used SQL to create relations. 像使用SQL创建关系时一样考虑它。 A lot of things are almost same. 许多事情几乎是相同的。

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

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