dousu1916 2014-08-31 18:44
浏览 61
已采纳

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

I have 2 tables like this:

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

The relation is ManyToOne (see the picture) and it's not required.

enter image description here

I've generated my entities automatically with Doctrine. In my player Entity I have:

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

But I don't have a $player variable in my Users Entity. In what way can I add this? Tried to do this but gave me different errors. What I'm trying to do is add a player form to my register form.

So in my RegisterType (=form) I woud like to add ->add('player', new PlayerType()). But that's not possible without a $player variable in my Users entity.

What type of relation do I need to setting for $player in my Users entity?

  • 写回答

1条回答 默认 最新

  • doubiao7410 2014-08-31 19:47
    关注

    You have to add annotation in user entity.

    In Player Enity

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

    In User Entity:

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

    This is only a draft. You have to write full namespaces for target entities, and correct errors if there are some.

    A lot of setails you can find here: Doctrine documentation - working with association

    Try to use small caps for table names, because MySQL under linux doesn't like uppercaps. $player must be instance of ArrayCollection

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 qt+ffmpeg报错non-existing PPS 0 referenced
  • ¥15 FOC simulink
  • ¥15 咨询一下有关于王者荣耀赢藏战绩
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?