0

What values goes inside this inversedBy annotation as well as the mappedBy annotation? Also what is targetEntity and referencedColumnName?

Here is an example of my 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.

/**
 * 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. 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.

Also,

Here is an example of where mappedBy is used:

/**
 * @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.

Any details on anything here would be great.

Any help would be great.

somejkuser
  • 8,856
  • 20
  • 64
  • 130

1 Answers1

1

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.

The inverse side has to use the mappedBy attribute of the OneToOne, OneToMany, or ManyToMany mapping declaration. The mappedBy attribute contains the name of the association-field on the owning side.

The owning side has to use the inversedBy attribute of the OneToOne, ManyToOne, or ManyToMany mapping declaration. The inversedBy attribute contains the name of the association-field on the inverse-side.

When you use @ORM\ManyToOne annotation you are creating n:1 relationship. There are three types of mapping:

  • bidirectional - Post with access to Comment and vice versa
  • 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
  • self-referencing - Category with with self-reference to parent Category which is entity of same type

TargetEntity tells to which entity you are creating relationship. Imagine foreign key. When you create foreign key you need to specify referencing table.

ReferencedColumnName tells to which column foreign key should be created.

Doctrine is not magic. It is just Object Relation Mapping. Think about it like when you used SQL to create relations. A lot of things are almost same.

Community
  • 1
  • 1
Northys
  • 1,305
  • 3
  • 16
  • 32