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

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

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误