DDC-1506: Possible Regression with OneToOne relation #1885

Open
opened 2026-01-22 13:29:43 +01:00 by admin · 0 comments
Owner

Originally created by @doctrinebot on GitHub (Nov 23, 2011).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user n3b:

/****
 * @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.

Originally created by @doctrinebot on GitHub (Nov 23, 2011). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user n3b: ``` /**** * @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.
admin added the BugInvalid labels 2026-01-22 13:29:43 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1885