Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined index: inverseJoinColumns while trying to define ManyToMany relationship between two entities

I have two entities: User and Company and the relationship between them is n:m. In my User.php entity I have this code:

/**
 * @ORM\ManyToMany(targetEntity="PL\CompanyBundle\Entity\Company", mappedBy="users", cascade={"all"})
 */
protected $companies;

public function __construct() {
    $this->companies = new \Doctrine\Common\Collections\ArrayCollection();
}

public function setCompanies(\PL\CompanyBundle\Entity\Company $companies) {
    $this->companies = $companies;
}

public function getCompanies() {
    return $this->companies;
}

And in my Company.php entity I have this other code:

/**
 * @ORM\ManyToMany(targetEntity="Application\Sonata\UserBundle\Entity\User", mappedBy="companies")
 */
protected $users;

public function __construct() {
    $this->users = new \Doctrine\Common\Collections\ArrayCollection();
}

But I got this error:

ContextErrorException: Notice: Undefined index: inverseJoinColumns in /var/www/html/apps/portal_de_logistica/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1041

What is wrong in the mapping?

EDIT Refactor code

Following instructions from @ahmed-siouani I made the following changes:

User.php

/**
 * @ORM\ManyToMany(targetEntity="PL\CompanyBundle\Entity\Company", inversedBy="users")
 * @ORM\JoinTable(name="fos_user_user_has_company",
 *      JoinColumns={@ORM\JoinColumn(name="fos_user_user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="company_id", referencedColumnName="id")}
 * )
 */
protected $companies;

where fos_user_user_has_company is the table added for the n:m relationship.

Company.php

/**
 * @ORM\ManyToMany(targetEntity="Application\Sonata\UserBundle\Entity\User", mappedBy="companies")
 */
protected $users;

And now the error is:

AnnotationException: [Creation Error] The annotation @ORM\JoinTable declared on property Application\Sonata\UserBundle\Entity\User::$companies does not have a property named "JoinColumns". Available properties: name, schema, joinColumns, inverseJoinColumns

Any?

like image 931
Reynier Avatar asked Feb 13 '14 15:02

Reynier


2 Answers

My fault was that both sides of the ManyToMany had been defined with mappedBy, but only one side should have been using mappedBy and the other side should have used inversedBy (this is normally defined at main entity, that controls the collection).

like image 128
Tobias Avatar answered Sep 27 '22 01:09

Tobias


You may need to specify the joinColumns and the inverseJoinColumns when defining thejoinTable. For a bidirectional many-to-many definition is would be something like,

class User
{
    // ...

    /**
     * Bidirectional - Many users have Many companies (OWNING SIDE)
     *
     * @ManyToMany(targetEntity="Company", inversedBy="users")
     * @JoinTable(name="users_companies",
     *     joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *     inverseJoinColumns={@ORM\JoinColumn(name="company_id", referencedColumnName="id")}
     * )
     **/
    private $companies;

    public function __construct() {
        $this->companies = new \Doctrine\Common\Collections\ArrayCollection();
    }

    // ...
}

While your Company class should be defined as follow,

class Company
{
    // ...

    /**
     * Bidirectional (INVERSE SIDE)
     *
     * @ManyToMany(targetEntity="User", mappedBy="companies")
     */
    private $users;
like image 28
Ahmed Siouani Avatar answered Sep 26 '22 01:09

Ahmed Siouani