我有两个实体:用户和公司,它们之间的关系是n:m。在我的User.php
实体中,我有以下代码:
/**
* @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:注意:未定义的索引:inverseJoinColumns in /var/www/html/apps/portal_de_logistica/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php 第1041行
映射有什么问题?
编辑重构代码
按照@ ahmed-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]注释@ORM \ JoinTable 在财产上宣布 Application \ Sonata \ UserBundle \ Entity \ User :: $ companies没有 名为“JoinColumns”的属性。可用属性:名称,架构, joinColumns,inverseJoinColumns
不限?
答案 0 :(得分:3)
在定义joinColumns
时,您可能需要指定inverseJoinColumns
和joinTable
。对于双向多对多定义,就像是,
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();
}
// ...
}
虽然您的公司类应定义如下,
class Company
{
// ...
/**
* Bidirectional (INVERSE SIDE)
*
* @ManyToMany(targetEntity="User", mappedBy="companies")
*/
private $users;
答案 1 :(得分:3)
我的错是ManyToMany的两边都是用mappedBy定义的,但是只有一方应该使用mappedBy而另一方应该使用inversedBy(这通常是在主实体上定义,控制集合)。
答案 2 :(得分:0)
除了@Ahmed解决方案之外我还处理了一个错别字,因为我做了第二个错误。看我的注释说:
/**
* @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;
但是正确的是:
/**
* @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;
差异在于这一行:
joinColumns={@ORM\JoinColumn(name="fos_user_user_id", referencedColumnName="id")},