加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 16:31:41 所属栏目:PHP教程 来源:网络整理
导读:在这个关联映射页面上,在manytomany部分有一个例子.但我不明白哪个实体(组或用户)是拥有方. http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html#many-to-many-bidirectional 我也把代码放在这里 ?php/** @Entity */class User{
在这个关联映射页面上,在manytomany部分有一个例子.但我不明白哪个实体(组或用户)是拥有方.

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

我也把代码放在这里

<?php
/** @Entity */
class User
{
    // ...

    /**
     * @ManyToMany(targetEntity="Group",inversedBy="users")
     * @JoinTable(name="users_groups")
     */
    private $groups;

    public function __construct() {
        $this->groups = new DoctrineCommonCollectionsArrayCollection();
    }

    // ...
}

/** @Entity */
class Group
{
    // ...
    /**
     * @ManyToMany(targetEntity="User",mappedBy="groups")
     */
    private $users;

    public function __construct() {
        $this->users = new DoctrineCommonCollectionsArrayCollection();
    }

    // ...
}

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

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

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

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

/**
 * @ManyToMany(targetEntity="Group",inversedBy="users")
 * @JoinTable(name="users_groups")
 */
private $groups;

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

/**
 * @ManyToMany(targetEntity="User",mappedBy="groups")
 */
private $users;

mappingBy是什么意思?

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读