dreamy1992 2017-01-13 09:58
浏览 59
已采纳

Doctrine2错误地级联了oneToMany关系

I have 2 doctrine entities:
Commessa

/**
 * @ORM\Id
 * @ORM\Column(type = "bigint")
 * @ORM\GeneratedValue(strategy = "AUTO")
 */
private $id;

/* ... */

/*
 * @ORM\OneToMany(targetEntity = "AppBundle\Entity\Pipeline\pipeline", mappedBy = "commessa", cascade = {"persist", "remove"})
 */
private $pipelines;

and Pipeline

/**
 * @ORM\Id
 * @ORM\Column(type = "bigint")
 * @ORM\GeneratedValue(strategy = "AUTO")
 */
private $id;

/* ... */

/**
 * @ORM\ManyToOne(targetEntity = "AppBundle\Entity\Commessa\commessa", inversedBy = "pipelines")
 * @ORM\JoinColumn(name = "id_commessa", name = "id")
 */
private $commessa;

As you can see, both entities have an AUTO-INCREMENT, single field primary key called id, and a bidirectional association to the other; pipeline gets automatically persisted whenever i do so with commessa.
Furthermore, both entities only have a getter method for the id, and not a setter one.

Now, whenever i try to flush an object instance of the class Commessa including more than a single pipeline, the following error pops up:

An exception occurred while executing 'INSERT INTO pipeline (descrizione, nome_logico, id) VALUES (?, ?, ?)' with params ["", "frontend", "9"]:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '9' for key 'PRIMARY'

At no point in my code i set the pipeline's id, and dumping the Commessa object right before the flush (and after the persist) shows that it's populated correctly, and the pipelines have "null" as id, which i guess is correct.

Through the Symfony profiler, the following queries are reported:

"START_TRANSACTION"
INSERT INTO commessa (codice_commessa, data_creazione, data_scadenza, descrizione, id_anagrafica, id_cliente, id_stato, id_tipo_commesa) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
Parameters: [ 1 => lyme, 2 => 2017-01-13 10:47:53, 3 => 2017-01-17 00:00:00, 4 => Fai Lyme, 5 => 1, 6 => 1, 7 => 1, 8 => 1 ] 

INSERT INTO pipeline (descrizione, nome_logico, id) VALUES (?, ?, ?)
Parameters: [1 => , 2 => frontend, 3 => 10] 

INSERT INTO pipeline (descrizione, nome_logico, id) VALUES (?, ?, ?)
Parameters: [1 => , 2 => backend, 3 => 10] 
"ROLLBACK"

I then stumbled upon the doctrine limitations and known issues page, stating at point 28.1.3

There are two bugs now that concern the use of cascade merge in combination with bi-directional associations. but the related ticket link is dead.

Could this be my problem? If it is, how can i solve this problem?

  • 写回答

1条回答 默认 最新

  • dthyxna3894 2017-01-13 10:20
    关注

    @ORM\JoinColumn(name = "id_commessa", name = "id") is wrong. there is 2 times name field

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

报告相同问题?

悬赏问题

  • ¥40 如果update 一个列名为参数的value
  • ¥15 基于51单片机的水位检测系统设计中LCD1602一直不显示
  • ¥15 OCS2安装出现问题,请大家给点意见
  • ¥15 ros小车启动launch文件报错
  • ¥15 vs2015到期想登陆但是登陆不上
  • ¥15 IPQ5018制作烧录固件,boot运行失败(操作系统-linux)(相关搜索:操作系统)(相关搜索:操作系统)
  • ¥20 icefall在librispeech基础上加入个人数据集
  • ¥30 keepalive高可用故障运维配置询问
  • ¥15 求帮助!国家电网内网u盘突然识别不出来了。
  • ¥15 matlab语音变速变调同时实现