gpt4 book ai didi

symfony - 未定义索引 : inverseJoinColumns while trying to define ManyToMany relationship between two entities

转载 作者:行者123 更新时间:2023-12-04 23:51:13 30 4
gpt4 key购买 nike

我有两个实体:用户和公司,它们之间的关系是 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: Notice: Undefined index: inverseJoinColumns in /var/www/html/apps/portal_de_logistica/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 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] 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



任何?

最佳答案

我的错是 ManyToMany 的两侧都用mappedBy 定义,但只有一侧应该使用mappedBy,另一侧应该使用inversedBy(这通常在控制集合的主实体中定义)。

关于symfony - 未定义索引 : inverseJoinColumns while trying to define ManyToMany relationship between two entities,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21757875/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com