我有兩個實體:User和Company,它們之間的關係是n:m。在我User.php
實體我有這樣的代碼:未定義索引:inverseJoinColumns嘗試定義兩個實體之間的ManyToMany關係
/**
* @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;
}
在我Company.php
實體我有這樣的其他代碼:
/**
* @ORM\ManyToMany(targetEntity="Application\Sonata\UserBundle\Entity\User", mappedBy="companies")
*/
protected $users;
public function __construct() {
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
}
但我得到這個錯誤:
ContextErrorException: Notice: Undefined index: inverseJoinColumns in /var/www/html/apps/portal_de_logistica/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1041
什麼是錯的在映射?
編輯重構代碼
從@艾哈邁德 - siouani我做了以下修改以下說明:
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;
其中fos_user_user_has_company
是表添加了n:m
關係。
Company.php
/**
* @ORM\ManyToMany(targetEntity="Application\Sonata\UserBundle\Entity\User", mappedBy="companies")
*/
protected $users;
而現在的錯誤是:
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
任何?
只是爲了增加這一點,我有一面映射m:1和一面m:m,這引起了我的問題。謝謝@Tobias –