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

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

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题