你好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();
答案 0 :(得分:0)
从我的示例中我可以看出,你没有持久化任何实体,因此Doctrine没有注册任何更改,而且flush没有任何更新。
你必须坚持这段关系的拥有方。
$this->em->persist($service);
或
$this->em->persist($user);
取决于您指定的拥有方。