Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between inversedBy and mappedBy?

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:

  • example 1
  • example 2
  • example 3
like image 543
Developer Avatar asked Sep 19 '12 11:09

Developer


4 Answers

  • 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 https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/unitofwork-associations.html

like image 169
Andreas Linden Avatar answered Nov 06 '22 02:11

Andreas Linden


The answers above were not sufficient for me to understand what was going on, so after delving into it more I think I have a way of explaining it that will make sense for people who struggled like I did to understand.

inversedBy and mappedBy are used by the INTERNAL DOCTRINE engine to reduce the number of SQL queries it has to do to get the information you need. To be clear if you don't add inversedBy or mappedBy your code will still work but will not be optimized.

So for example, look at the classes below:

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

    /**
     * @var string
     *
     * @ORM\Column(name="task", type="string", length=255)
     */
    private $task;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="dueDate", type="datetime")
     */
    private $dueDate;

    /**
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="tasks", cascade={"persist"})
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     */
    protected $category;
}

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

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @ORM\OneToMany(targetEntity="Task", mappedBy="category")
     */
    protected $tasks;
}

These classes if you were to run the command to generate the schema (for example, bin/console doctrine:schema:update --force --dump-sql) you will notice that the Category table does not have a column on it for tasks. (this is because it does not have a column annotation on it)

The important thing to understand here is that the variable tasks is only there so the internal doctrine engine can use the reference above it which says its mappedBy Category. Now... don't be confused here like I was... Category is NOT referring TO THE CLASS NAME, its referring to the property on the Task class called 'protected $category'.

Like wise, on the Tasks class the property $category mentions it is inversedBy="tasks", notice this is plural, this is NOT THE PLURAL OF THE CLASS NAME, but just because the property is called 'protected $tasks' in the Category class.

Once you understand this it becomes very easy to understand what inversedBy and mappedBy are doing and how to use them in this situation.

The side that is referencing the foreign key like 'tasks' in my example always gets the inversedBy attribute because it needs to know what class (via the targetEntity command) and what variable (inversedBy=) on that class to 'work backwards' so to speak and get the category information from. An easy way to remember this, is the class that would have the foreignkey_id is the one that needs to have inversedBy.

Where as with category, and its $tasks property (which is not on the table remember, just only part of the class for optimization purposes) is MappedBy 'tasks', this creates the relationship officially between the two entities so that doctrine can now safely use JOIN SQL statements instead of two separate SELECT statements. Without mappedBy, the doctrine engine would not know from the JOIN statement it will create what variable in the class 'Task' to put the category information.

Hope this explains it a bit better.

like image 25
Joseph Astrahan Avatar answered Nov 06 '22 01:11

Joseph Astrahan


In bidirectional relationship has both an owning side and an inverse side

mappedBy : put into The inverse side of a bidirectional relationship To refer to the field in the owning side of entity

inversedBy : put into The owning side of a bidirectional relationship To refer to the field on the inverse side of entity

AND

mappedBy attribute used with the OneToOne, OneToMany, or ManyToMany mapping declaration.

inversedBy attribute used with the OneToOne, ManyToOne, or ManyToMany mapping declaration.

Notice : The owning side of a bidirectional relationship the side that contains the foreign key.

there two reference about inversedBy and mappedBy into Doctrine Documentation : First Link,Second Link

like image 23
ahmed hamdy Avatar answered Nov 06 '22 03:11

ahmed hamdy


5.9.1. Owning and Inverse Side

For Many-To-Many associations you can chose which entity is the owning and which the inverse side. There is a very simple semantic rule to decide which side is more suitable to be the owning side from a developers perspective. You only have to ask yourself, which entity is responsible for the connection management and pick that as the owning side.

Take an example of two entities Article and Tag. Whenever you want to connect an Article to a Tag and vice-versa, it is mostly the Article that is responsible for this relation. Whenever you add a new article, you want to connect it with existing or new tags. Your create Article form will probably support this notion and allow to specify the tags directly. This is why you should pick the Article as owning side, as it makes the code more understandable:

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html

like image 2
Micheal Mouner Mikhail Youssif Avatar answered Nov 06 '22 02:11

Micheal Mouner Mikhail Youssif