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.

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

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题