Details
-
Type:
Bug
-
Status:
Resolved
-
Priority:
Major
-
Resolution: Won't Fix
-
Affects Version/s: 2.2.2
-
Fix Version/s: None
-
Component/s: ORM
-
Security Level: All
-
Labels:
-
Environment:Win 7, PHP 5.3, MySQL InnoDB
Description
<?php
/** @Entity */
class A
{
/**
* @Id @Column(name="id", type="integer")
*/
private $id;
/**
* @OneToOne(targetEntity="B", mappedBy="a")
*/
private $b;
}
/**
* @Entity
* @InheritanceType("SINGLE_TABLE")
*/
class B
{
/**
* @Id @Column(name="id", type="integer", nullable=false)
*/
private $id;
/**
* @OneToOne(targetEntity="A", inversedBy="b")
*/
private $a;
}
/**
* @Entity
*/
class B_sub extends B
{
/** @Column(type="string") */
private $sub;
}
/**
* @Entity
*/
class B_sub2 extends B
{
/** @Column(type="string") */
private $sub2;
}
The code creates several tables and a UNIQUE KEY constraint on B (a_id)
Now trying to accomplish the following sequence of action:
$B = new B_sub();
$A = new A();
$A->set('b', $B);
$B->set('a', $A);
$em->persist($A);
$em->persist($B);
//works fine and creates the correct DB entries
$em->flush();
$B_new = new B_sub2();
$B_old = $A->get('b');
$A->set('b', $B_new);
$B_new->set('a', $A);
$em->remove($B_old);
$em->persist($A);
$em->persist($B_new);
//does not work, because B_new is inserted first and fails the UNIQUE KEY constraint (both B_new and B_old refer to A)
$em->flush();
The result is a "Duplicate entry '1' for key 'UNIQ_7F6BFCEBE26CCE03'"
Current workaround is to change the ownership of the relationship
<?php /** @Entity */ class A { /** * @Id @Column(name="id", type="integer") */ private $id; /** * @OneToOne(targetEntity="B", inversedBy="a") */ private $b; } /** * @Entity * @InheritanceType("SINGLE_TABLE") */ class B { /** * @Id @Column(name="id", type="integer", nullable=false) */ private $id; /** * @OneToOne(targetEntity="A", mappedBy="b") */ private $a; }