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?
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).
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With