【问题标题】:Doctrine2 incorrectly cascading oneToMany relationshipDoctrine2 错误地级联 oneToMany 关系
【发布时间】:2017-01-13 09:58:34
【问题描述】:

我有 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;

如您所见,两个实体都有一个 AUTO-INCREMENT、称为 id 的单字段主键,以及与另一个实体的双向关联;每当我使用 commessa 执行此操作时,管道都会自动持久化。
此外,这两个实体都只有一个用于 id 的 getter 方法,而不是一个 setter 方法。

现在,每当我尝试刷新包含多个管道的 Commessa 类的对象实例时,都会弹出以下错误:

使用参数 ["", "frontend", "9"] 执行“INSERT INTO pipeline (descrizione, nome_logico, id) VALUES (?, ?, ?)' 时发生异常:

SQLSTATE[23000]:违反完整性约束:1062 键 'PRIMARY' 的重复条目 '9'

在我的代码中,我没有设置管道的 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 点声明

现在有两个错误涉及将级联合并与双向关联结合使用。 但相关的票证链接已失效。

这可能是我的问题吗? 如果是,我该如何解决这个问题?

【问题讨论】:

  • 我假设这两个对象实际上不是同一个对象,添加了两次? (因为他们所有的属性都是一样的)
  • 对不起,我错误地复制了两次相同的查询;第二个字段不同。我编辑了问题。
  • * @ORM\JoinColumn(name = "id_commessa", name = "id") 是错误的。有2次名称字段
  • @JasminMistry 谢谢你的回答;证明你是对的
  • 请接受回答

标签: php doctrine-orm symfony one-to-many


【解决方案1】:

@ORM\JoinColumn(name = "id_commessa", name = "id") 是错误的。有2次名称字段

【讨论】:

    猜你喜欢
    • 2011-06-22
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    • 1970-01-01
    相关资源
    最近更新 更多