DDC-2383: Foreign relations on primary keys don't work on more than two entities (like Foo<>Bar<>Baz) #2993

Closed
opened 2026-01-22 14:09:12 +01:00 by admin · 3 comments
Owner

Originally created by @doctrinebot on GitHub (Apr 1, 2013).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user extreme:

I'm trying to accomplish something like this:
http://docs.doctrine-project.org/en/latest/tutorials/composite-primary-keys.html#use-case-2-simple-derived-identity

For two entities (Foo<>Bar) it works as expected but adding another entity related to Bar (so it's Foo<>Bar<>Baz) ends up with this error:

Fatal error: Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'The column id must be mapped to a field in class Entity\Bar since it is referenced by a join column of another class.' in C:\...\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\MappingException.php:203
Stack trace:
#0 C:\...\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\BasicEntityPersister.php(734): Doctrine\ORM\Mapping\MappingException::joinColumnMustPointToMappedField('Entity\Bar', 'id')
#1 C:\...\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(2509): Doctrine\ORM\Persisters\BasicEntityPersister->loadOneToOneEntity(Array, Object(Entity\Bar))
#2 C:\...\vendor\doctrine\orm\lib\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php(245): Doctrine\ORM\UnitOfWork->createEntity('Entity\Bar', Array, Array)
#3 C:\...\vendor\doctrine\orm\lib\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php(424): Doctrine\ORM\Internal\Hydration\Ob in C:\...\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\MappingException.php on line 203

This error appears when there are some records in the database and I want to query for example all Foos.

My entites look like this:

//Entity/Foo.php

/*** @Entity @Table(name="foos") **/
class Foo
{
    /*** @Id @Column(type="integer") @GeneratedValue **/
    protected $id;

    /*** @OneToOne(targetEntity="Bar", mappedBy="foo") **/
    protected $bar;

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

    public function getBar()
    {
        return $this->bar;
    }

    public function setBar($bar)
    {
        $bar->setFoo($this);
        $this->bar = $bar;
    }
}

//Entity/Bar.php

/*** @Entity @Table(name="bars") **/
class Bar
{
    /**** @Id @OneToOne(targetEntity="Foo", inversedBy="bar")
     ** @JoinColumn(name="id", referencedColumnName="id") **/
    protected $foo;

    /*** @OneToOne(targetEntity="Baz", mappedBy="bar") **/
    protected $baz;

    public function **construct($foo)
    {
        $this->foo = $foo;
    }

    public function getId()
    {
        return $this->getFoo()->getId();
    }

    public function getFoo()
    {
        return $this->foo;
    }

    public function setFoo($foo)
    {
        $this->foo = $foo;
    }

    public function getBaz()
    {
        return $this->baz;
    }

    public function setBaz($baz)
    {
        $bar->setBar($this);
        $this->baz = $baz;
    }
}

//Entity/Baz.php

/*** @Entity @Table(name="bazes") **/
class Baz
{
    /**** @Id @OneToOne(targetEntity="Bar", inversedBy="baz")
     ** @JoinColumn(name="id", referencedColumnName="id") **/
    protected $bar;

    public function **construct($bar)
    {
        $this->bar = $bar;
    }

    public function getId()
    {
        return $this->getBar()->getId();
    }

    public function getBar()
    {
        return $this->bar;
    }

    public function setBar($bar)
    {
        $this->bar = $bar;
    }
}

And fails on

$fooRepository = $em->getRepository('Entity\Foo');
$foos = $fooRepository->findAll();
Originally created by @doctrinebot on GitHub (Apr 1, 2013). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user extreme: I'm trying to accomplish something like this: http://docs.doctrine-project.org/en/latest/tutorials/composite-primary-keys.html#use-case-2-simple-derived-identity For two entities (Foo<>Bar) it works as expected but adding another entity related to Bar (so it's Foo<>Bar<>Baz) ends up with this error: ``` Fatal error: Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'The column id must be mapped to a field in class Entity\Bar since it is referenced by a join column of another class.' in C:\...\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\MappingException.php:203 Stack trace: #0 C:\...\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\BasicEntityPersister.php(734): Doctrine\ORM\Mapping\MappingException::joinColumnMustPointToMappedField('Entity\Bar', 'id') #1 C:\...\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(2509): Doctrine\ORM\Persisters\BasicEntityPersister->loadOneToOneEntity(Array, Object(Entity\Bar)) #2 C:\...\vendor\doctrine\orm\lib\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php(245): Doctrine\ORM\UnitOfWork->createEntity('Entity\Bar', Array, Array) #3 C:\...\vendor\doctrine\orm\lib\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php(424): Doctrine\ORM\Internal\Hydration\Ob in C:\...\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\MappingException.php on line 203 ``` This error appears when there are some records in the database and I want to query for example all Foos. My entites look like this: ``` //Entity/Foo.php /*** @Entity @Table(name="foos") **/ class Foo { /*** @Id @Column(type="integer") @GeneratedValue **/ protected $id; /*** @OneToOne(targetEntity="Bar", mappedBy="foo") **/ protected $bar; public function getId() { return $this->id; } public function getBar() { return $this->bar; } public function setBar($bar) { $bar->setFoo($this); $this->bar = $bar; } } //Entity/Bar.php /*** @Entity @Table(name="bars") **/ class Bar { /**** @Id @OneToOne(targetEntity="Foo", inversedBy="bar") ** @JoinColumn(name="id", referencedColumnName="id") **/ protected $foo; /*** @OneToOne(targetEntity="Baz", mappedBy="bar") **/ protected $baz; public function **construct($foo) { $this->foo = $foo; } public function getId() { return $this->getFoo()->getId(); } public function getFoo() { return $this->foo; } public function setFoo($foo) { $this->foo = $foo; } public function getBaz() { return $this->baz; } public function setBaz($baz) { $bar->setBar($this); $this->baz = $baz; } } //Entity/Baz.php /*** @Entity @Table(name="bazes") **/ class Baz { /**** @Id @OneToOne(targetEntity="Bar", inversedBy="baz") ** @JoinColumn(name="id", referencedColumnName="id") **/ protected $bar; public function **construct($bar) { $this->bar = $bar; } public function getId() { return $this->getBar()->getId(); } public function getBar() { return $this->bar; } public function setBar($bar) { $this->bar = $bar; } } ``` And fails on ``` $fooRepository = $em->getRepository('Entity\Foo'); $foos = $fooRepository->findAll(); ```
admin added the Bug label 2026-01-22 14:09:12 +01:00
admin closed this issue 2026-01-22 14:09:14 +01:00
Author
Owner

@doctrinebot commented on GitHub (Apr 1, 2013):

Comment created by extreme:

Attaching a test case which results in two exceptions - while creating the schema and while fetching entities.

@doctrinebot commented on GitHub (Apr 1, 2013): Comment created by extreme: Attaching a test case which results in two exceptions - while creating the schema and while fetching entities.
Author
Owner

@doctrinebot commented on GitHub (Apr 14, 2013):

Comment created by @beberlei:

This is sadly a restriction of the foreign keys as primary key feature.

Due to the architecture of shared nothing Metadata instances we cannot validate this at mapping compile time, only at runtime, thus leading to this error.

@doctrinebot commented on GitHub (Apr 14, 2013): Comment created by @beberlei: This is sadly a restriction of the foreign keys as primary key feature. Due to the architecture of shared nothing Metadata instances we cannot validate this at mapping compile time, only at runtime, thus leading to this error.
Author
Owner

@doctrinebot commented on GitHub (Apr 14, 2013):

Issue was closed with resolution "Can't Fix"

@doctrinebot commented on GitHub (Apr 14, 2013): Issue was closed with resolution "Can't Fix"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#2993