【问题标题】:How to update an object with ManyToMany relation with doctrine 2如何更新具有多对多关系的对象与学说 2
【发布时间】:2011-12-08 11:44:38
【问题描述】:

您好 Stack Overflow 成员! 我安装了学说 2,一切正常, 我已经使用命令生成了我的实体和代理,

我的问题是当我尝试更新具有多对多关系的实体时 我遇到了这个问题

致命错误:带有消息的未捕获异常“PDOException” 'SQLSTATE [23000]:完整性约束违规:1062 重复条目 '8-1' 表示键 'PRIMARY''

似乎学说尝试插入新实体而不是尝试更新连接表 如果我的代码有任何问题,是否有明确的示例? 谢谢

    //update entities user


     $user=$this->em->getRepository('Entities\User')->find((int)$this->input->post('id'));

     $user->setNom($this->input->post('nom'));
     $user->setPrenom($this->input->post('prenom'));

    //update entities services(user have many service)

    foreach($this->input->post('services') as $post){
        $service = $this->em->getRepository('Entities\Service')->find((int)$post);
              if ($service instanceof Entities\Service) {
                  $user->addService($service);
        }
    $this->em->flush();

【问题讨论】:

  • foreach 循环缺少右花括号},请告诉您的表的主键是什么

标签: doctrine-orm


【解决方案1】:

据我从您的示例中可以看出,您没有持久化任何实体,因此 Doctrine 没有注册任何更改,并且 flush 没有任何要更新的内容。

你必须坚持关系的拥有方。

$this->em->persist($service);

$this->em->persist($user);

取决于您指定的拥有方。

查看documentation

【讨论】:

  • $service 和 $user 是从“em”中检索到的,它们正在被管理,您不需要调用 persist()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-21
  • 2012-05-04
  • 1970-01-01
  • 1970-01-01
  • 2017-09-26
  • 2019-10-16
  • 1970-01-01
相关资源
最近更新 更多