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?