6

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:

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

[Doctrine\ORM\Mapping\MappingException]
OneToMany mapping on field 'address' requires the 'mappedBy' attribute.

/**
 * 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? 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. Just as the documentation says, but it was a bit confusing for me because there is already a @OneToMay annotation. So I just have to change the above code to this (by only changing the @OneToMany to @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;
5
  • I don't really understand why you want a JoinTable for a OneToMany association, but I may miss something ?
    – tchap
    Oct 13, 2015 at 17:00
  • For nothing specific. I am just learning about doctrine.
    – mevqz
    Oct 13, 2015 at 17:07
  • Then you can drop it I guess. OneToMany association are bidirectional by default as well, so you need a mappedBy and inversedBy, see doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/…. Your unidirectional OneToMany is just a ManyToMany in disguise
    – tchap
    Oct 13, 2015 at 17:12
  • thanks, now I get it.
    – mevqz
    Oct 13, 2015 at 19:04
  • 1
    @tchap to me A JoinTable for a OneToMany association makes sense if the entities on the Many side can be owned by different type of entities. In this case the typical foreign key on this side won't be enough to distinguish the owner unless you use UUID for all your entities.
    – Remi M
    Sep 8, 2016 at 16:36

1 Answer 1

15

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. The owning side is 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:

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

/** 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(); 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.

/**
 * @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). Such example can also be found here in the Doctrine documentation chapter 5.6. One-To-Many, Unidirectional with Join Table.

5
  • Thanks. You got a typo for the owning side (it should be ManyToOne instead OneToMany), but this is the correct answer.
    – mevqz
    Oct 15, 2015 at 19:31
  • @mevqz oops, fixed it :P
    – Wilt
    Oct 15, 2015 at 19:59
  • @Wilt In your last example, I suppose you talk about the User entity since the target entity is Address. There's a typo, the address_id with the unique =true property is in the inverseJoinColumns, not the joinColumns :)
    – Remi M
    Sep 8, 2016 at 16:20
  • 1
    @Azuli42 Very true, thanks! I fixed it and added a link to the documentation. I copied the annotation from the question, so the same mistake was made there.
    – Wilt
    Sep 9, 2016 at 7:22
  • 1
    @Azuli42 I edited the question and fixed it there too.
    – Wilt
    Sep 9, 2016 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.