dongxi7722 2012-11-12 15:12 采纳率: 0%
浏览 34
已采纳

在doctrine 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-to-many-bidirectional

I've put the code here too

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

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

  • 写回答

2条回答 默认 最新

  • duanph1978 2012-11-13 01:07
    关注

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

    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;
    

    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;
    

    What does mappedBy mean?

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 如何在不能联网影子模式下的电脑解决usb锁
  • ¥20 服务器redhat5.8网络问题
  • ¥15 如何利用c++ MFC绘制复杂网络多层图
  • ¥20 要做柴油机燃烧室优化 需要保持压缩比不变 请问怎么用AVL fire ESE软件里面的 compensation volume 来使用补偿体积来保持压缩比不变
  • ¥15 python螺旋图像
  • ¥15 算能的sail库的运用
  • ¥15 'Content-Type': 'application/x-www-form-urlencoded' 请教 这种post请求参数,该如何填写??重点是下面那个冒号啊
  • ¥15 找代写python里的jango设计在线书店
  • ¥15 请教如何关于Msg文件解析
  • ¥200 sqlite3数据库设置用户名和密码