php – 在doctrine 2 doc示例中,拥有方和反面是什么

前端之家收集整理的这篇文章主要介绍了php – 在doctrine 2 doc示例中,拥有方和反面是什么前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在这个关联映射页面上,在manytomany部分有一个例子.但我不明白哪个实体(组或用户)是拥有方.

http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html#many-to-many-bidirectional

我也把代码放在这里

  1. <?PHP
  2. /** @Entity */
  3. class User
  4. {
  5. // ...
  6.  
  7. /**
  8. * @ManyToMany(targetEntity="Group",inversedBy="users")
  9. * @JoinTable(name="users_groups")
  10. */
  11. private $groups;
  12.  
  13. public function __construct() {
  14. $this->groups = new \Doctrine\Common\Collections\ArrayCollection();
  15. }
  16.  
  17. // ...
  18. }
  19.  
  20. /** @Entity */
  21. class Group
  22. {
  23. // ...
  24. /**
  25. * @ManyToMany(targetEntity="User",mappedBy="groups")
  26. */
  27. private $users;
  28.  
  29. public function __construct() {
  30. $this->users = new \Doctrine\Common\Collections\ArrayCollection();
  31. }
  32.  
  33. // ...
  34. }

我这样读这个注释:
用户被映射为组,所以组是进行连接管理的实体,因此拥有方?

此外,我已经在文档中阅读过:

  1. 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).

这让我认为,在该实体中定义了JoinTable注释时,用户将成为所有者.

But I don’t understand which entity (group or user) is the owning side

用户实体是所有者.您在用户中具有组的关系:

  1. /**
  2. * @ManyToMany(targetEntity="Group",inversedBy="users")
  3. * @JoinTable(name="users_groups")
  4. */
  5. private $groups;

看看上面,$groups var包含与此用户关联的所有组,但如果您注意到属性定义,$groups var具有与以前一样的mappedBy值(mappedBy =“groups”)的相同名称

  1. /**
  2. * @ManyToMany(targetEntity="User",mappedBy="groups")
  3. */
  4. private $users;

mappingBy是什么意思?

This option specifies the property name on the targetEntity that is the owning side of this relation.

猜你在找的PHP相关文章