gpt4 book ai didi

php - Symfony 2 Doctrine 2 使用 inversedBy 和 mappedBy 进入多对多关系?

转载 作者:搜寻专家 更新时间:2023-10-31 20:41:55 25 4
gpt4 key购买 nike

进入Doctrine 2 Documentation将 Owning Side 和 Inverse Side 解释为 ManyToMany 关系并说:

For ManyToMany bidirectional relationships either side may be the owning side (the side that defines the @JoinTable and/or does not make use of the mappedBy attribute, thus using a default join table).

这是否意味着我可以在不使用 inversedBy 和 mappedBy 的情况下编写注释引用多对多关系的拥有方和多对多关系的反向方?

例如:

我可以这样写吗:

关联实体

   /**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Category")
*
*/
private $categories;

类别实体

/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Affiliate")
* @ORM\JoinTable(name="category_affiliate")
*
*/
private $afflitiates;

而不是这样写:

关联实体

   /**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Category", mappedBy="afflitiates")
*
*/
private $categories;

类别实体

/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Affiliate",inversedBy="categories")
* @ORM\JoinTable(name="category_affiliate")
*
*/
private $afflitiates;

最佳答案

在多对多关系的情况下,您可以选择任何一方作为您的拥有方,另一方自动变为反转方。但是请尝试检查您将频繁触发哪个实体来获取对象并管理您拥有的一方

关于php - Symfony 2 Doctrine 2 使用 inversedBy 和 mappedBy 进入多对多关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20101685/

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