Doctrine2 错误地级联 oneToMany 关系
Doctrine2 incorrectly cascading oneToMany relationship
我有 2 个学说实体:
科梅萨
/**
* @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;
和管道
/**
* @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;
如您所见,两个实体都有一个自动递增、名为 id 的单字段主键,以及与另一个实体的双向关联;每当我使用 commessa 时,管道都会自动保留。
此外,两个实体都只有一个 getter 方法用于 id,而不是一个 setter 方法。
现在,每当我尝试刷新包含多个管道的 class Commessa 的对象实例时,都会弹出以下错误:
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'
在我的代码中,我没有设置管道的 ID,并且在刷新之前(和持久化之后)转储 Commessa 对象表明它已正确填充,并且管道的 ID 为“null”,我猜对了。
通过 Symfony 分析器,报告了以下查询:
"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"
然后我偶然发现了 the doctrine limitations and known issues page,在第 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.
会不会是我的问题?
如果是,我该如何解决这个问题?
@ORM\JoinColumn(name = "id_commessa", name = "id") 是错误的。有 2 次名称字段
我有 2 个学说实体:
科梅萨
/**
* @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;
和管道
/**
* @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;
如您所见,两个实体都有一个自动递增、名为 id 的单字段主键,以及与另一个实体的双向关联;每当我使用 commessa 时,管道都会自动保留。
此外,两个实体都只有一个 getter 方法用于 id,而不是一个 setter 方法。
现在,每当我尝试刷新包含多个管道的 class Commessa 的对象实例时,都会弹出以下错误:
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'
在我的代码中,我没有设置管道的 ID,并且在刷新之前(和持久化之后)转储 Commessa 对象表明它已正确填充,并且管道的 ID 为“null”,我猜对了。
通过 Symfony 分析器,报告了以下查询:
"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"
然后我偶然发现了 the doctrine limitations and known issues page,在第 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.
会不会是我的问题? 如果是,我该如何解决这个问题?
@ORM\JoinColumn(name = "id_commessa", name = "id") 是错误的。有 2 次名称字段