douwei1944 2012-08-14 05:51
浏览 36
已采纳

如何在symfony2 Doctrine2中的构造函数中将实体添加到用户对象

I have the user object who has manytomany relation with Group entity.

But i want that by default the user should be added to Group Group_User which has id=4 in database.

Now how can i add that in User constructor

How can use query in Entity class

  • 写回答

1条回答 默认 最新

  • dongmao7195 2012-08-14 06:00
    关注

    You can pass what you want in your User constructor. You'll have to pass it from your controller (where your queryManger is available).

    In your controller:

    $group_user = $this->getDoctrine()->getEntityManager()->getRepository("Bundle:Entity")->find(4);
    $user = new User($group_user);
    

    In your construct:

    public function __construct(Group $group_user)
    {
        $this->$group = $group_user;
    }
    

    When you'll persist your user entity in the controller, the user and it's group relation will be directly saved.

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

报告相同问题?

悬赏问题

  • ¥20 数学建模数学建模需要
  • ¥15 c语言数据结构实验单链表的删除
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决