原则2 doc示例中的拥有侧和反向侧是什么 [英] What is the owning side and inverse side in the doctrine 2 doc example

查看:59
本文介绍了原则2 doc示例中的拥有侧和反向侧是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在关联映射的此页面上,很多部分有一个示例。但是我不知道哪个实体(组或用户)是所有者。

On this page of association mapping, there's an example in the manytomany section. But I don't understand which entity (group or user) is the owning side.

http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html#many对多双向

我也将代码放在这里

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

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

    public function __construct() {
        $this->groups = new \Doctrine\Common\Collections\ArrayCollection();
    }

    // ...
}

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

    public function __construct() {
        $this->users = new \Doctrine\Common\Collections\ArrayCollection();
    }

    // ...
}

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

Do I read this annotation like this: User is mappedBy groups so group is the entity that does the connection management, thus the owning side?

也,我已经在文档中阅读了以下内容:

Also, I've read this in the docs:

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批注,因此User将是拥有方。

This lets me think that User would be the owning side as the JoinTable annotation is defined in that entity.

推荐答案


但是我不知道哪个实体(组或用户)是拥有方

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

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

The User entity is the owner. You have the relation of groups in User:

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

上面看, $ groups var包含与该用户关联的所有组,但是如果您注意到属性定义,则 $ groups var的名称相同 mappedBy 值(mappedBy = ),方法与您一样:

Look above, $groups var contains the all groups associated to this user, but If you notice the property definition, $groups var has the same name of mappedBy value (mappedBy="groups"), as you did:

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

mappedBy是什么意思?


此选项指定targetEntity上作为该关系拥有方的属性名称。

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

这篇关于原则2 doc示例中的拥有侧和反向侧是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆