Possible Regression with OneToOne relation (DDC-1461)

[DDC-1506] Possible Regression with OneToOne relation Created: 23/Nov/11  Updated: 23/Nov/11

Status: Open
Project: Doctrine 2 - ORM
Component/s: ORM
Affects Version/s: 2.1.2
Fix Version/s: None
Security Level: All

Type: Sub-task Priority: Major
Reporter: Maxim Assignee: Benjamin Eberlei
Resolution: Unresolved Votes: 0
Labels: None


 Description   
/**
 * @ORM\Entity
 */
class Top
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @ORM\OneToOne(targetEntity="LevelOne", orphanRemoval="true", cascade={"persist", "remove"})
     */
    protected $levelOne;
    
    public function getId()
    {
        return $this->id;
    }

    public function setLevelOne(LevelOne $levelOne)
    {
        $this->levelOne = $levelOne;
    }

    public function getLevelOne()
    {
        return $this->levelOne;
    }
}

/**
 * @ORM\Entity
 */
class LevelOne
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @ORM\OneToOne(targetEntity="LevelTwo", orphanRemoval="true", cascade={"persist", "remove"})
     */
    protected $levelTwo;

    public function getId()
    {
        return $this->id;
    }
    
    public function setId($id)
    {
        $this->id = $id;
    }

    public function setLevelTwo(LevelTwo $levelTwo)
    {
        $this->levelTwo = $levelTwo;
    }

    public function getLevelTwo()
    {
        return $this->levelTwo;
    }
}

/**
 * @ORM\Entity
 */
class LevelTwo
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    
    public function getId()
    {
        return $this->id;
    }
    
    public function setId($id)
    {
        $this->id = $id;
    }
}

trying to clone objects

$top = new Top();
        $top->setLevelOne(new LevelOne());
        $top->getLevelOne()->setLevelTwo(new LevelTwo());
        
        $this->em->persist($top);
        $this->em->flush();
        
        $newTop = new Top();
        $newTop->setLevelOne(clone $top->getLevelOne());
        $newTop->getLevelOne()->setId(null);
        $newTop->getLevelOne()->getLevelTwo()->setId(null);
        
        var_dump($newTop->getLevelOne()->getId());
        var_dump($newTop->getLevelOne()->getLevelTwo()->getId());
        
        $this->em->persist($newTop);
        $this->em->flush();

the output is:
NULL
NULL
[PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'UNIQ_82A72CD0778BC57F'
(it duplicates level two entity)
I worked for a while with entities, in a certain set of entity properties it completely persisted into database, but without relation between level one and level two.


Generated at Sat Oct 25 14:50:27 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.