【发布时间】:2021-09-26 17:54:08
【问题描述】:
那么,您能否向我解释一下为什么 Symfony 的命令 make:entity 会生成与 ManyToMany 关系不同的 addProperty 方法?
我花了几分钟试图了解原因,但还没有理解。
举例说明:
假设你有这两个类:
- 语言
- 国家
# Now running:
bin/console make:entity Country
# You'll enter in the interactive terminal, just type:
> languages
> ManyToMany
> Language
> yes
这些步骤将在Country 类中生成以下代码:
...
public function addLanguage(Language $language): self
{
if (!$this->languages->contains($language)) {
$this->languages[] = $language;
}
return $this;
}
...
在Language 课程中,你会得到这个:
...
public function addCountry(Country $country): self
{
if (!$this->countries->contains($country)) {
$this->countries[] = $country;
$country->addLanguage($this);
}
return $this;
}
...
我试图理解为什么Language 有$country->addLanguage($this); 而Country 没有。
【问题讨论】:
-
想想如果这样做会发生什么。或者试试看。
-
@Cerad 如果你猜是因为递归,我不认为是因为
!$this->countries->contains($country)。 -
这有助于@Cerad,谢谢!
-
@Cerad 根据 maker 命令,文档有点混乱。文档说
Article是Tag的所有者,所以Article是inverse side,Tag是map side。文档说:$tag->addArticle($this); // synchronously updating inverse side但Tag不是inverse side。看这里可以更好地理解:symfonycasts.com/screencast/doctrine-relations/many-to-many。因此,这一切对我帮助很大。谢谢!
标签: php symfony symfony4 php-7