ManyToOne关系+如何在两个方向上使用

时间:2014-08-31 18:44:47

标签: php symfony doctrine-orm relation

我有2张这样的表:

Users
    - UserID
    - Username
    - Password
    - ...
 Players
    - playerID
    - playerName
    - ...
    - User

关系是ManyToOne(见图),并不是必需的。

enter image description here

我已经使用Doctrine自动生成了我的实体。在我的播放器实体中我有:

 /**
 * @var \NV\VolleyScoutBundle\Entity\Users
 *
 * @ORM\ManyToOne(targetEntity="NV\VolleyScoutBundle\Entity\Users")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
 * })
 */
protected $user;

但我的用户实体中没有$player变量。我可以用什么方式添加这个?试图这样做,但给了我不同的错误。我想要做的是在我的注册表格中添加一个玩家表格。

所以在我的RegisterType(= form)中,我想添加->add('player', new PlayerType())。但是如果没有我的用户实体中的$ player变量,这是不可能的。

在我的用户实体中设置$ player需要什么类型的关系?

1 个答案:

答案 0 :(得分:1)

您必须在用户实体中添加注释。

在玩家恩惠中

@ORM\OneToMany(targetEntity="Players", mappedBy="user")
protected $player;

在用户实体中:

@ORM\ManyToOne(targetEntity="Users", inversedBy="player")
@ORM\JoinColumn(name="user_id", referendecColumn="UserId")
proteced $user;

这只是一个草案。您必须为目标实体编写完整的命名空间,并在存在错误时更正错误。

您可以在此处找到许多精彩内容:Doctrine documentation - working with association

尝试对表名使用小型大写字母,因为linux下的MySQL并不像上层大写。 $ player必须是ArrayCollection的实例