I have inherited a Symfony application where two OneToOne-entities share a primary key. This is modeled in two entity classes like this:
class A
{
/**
* @ORM\Id
* @ORM\OneToOne(targetEntity="App\Entity\B", inversedBy="a", cascade={"persist"}, fetch="EAGER")
* @ORM\JoinColumn(name="id", referencedColumnName="id", nullable=false)
*/
private B $b;
}
and
class B
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* @ORM\OneToOne(targetEntity="App\Entity\A", mappedBy="b", cascade={"persist"}, fetch="EAGER")
*/
private A $a;
}
which is not ideal but mostly works. Now there is another entity which has a more “traditional” OneToOne doctrine relationship with A:
class C
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* @ORM\OneToOne(targetEntity="App\Entity\A")
*/
private ?A $a = null;
}
But as soon as I try to use the repository class for C to fetch a C entity with find(1)
everything just falls apart: Typed property App\Entity\A::$b must be an instance of App\Entity\B, int used
I am pretty sure it is either https://github.com/doctrine/orm/issues/8255 or https://github.com/doctrine/orm/issues/8745 or probably both.
There are the versions of the libraries in my composer.lock
:
doctrine/annotations 1.13.2
doctrine/cache 2.1.1
doctrine/collections 1.6.8
doctrine/common 3.2.2
doctrine/dbal 2.13.8
doctrine/deprecations v0.5.3
doctrine/doctrine-bundle 2.5.7
doctrine/doctrine-migrations-bundle 3.2.2
doctrine/event-manager 1.1.1
doctrine/inflector 2.0.4
doctrine/instantiator 1.4.1
doctrine/lexer 1.2.3
doctrine/migrations 3.3.2
doctrine/orm 2.10.5
doctrine/persistence 2.4.0
doctrine/sql-formatter 1.1.2
Matthias Kühne
Yes thats the same issue. `A::b` is a foreign key to `B` AND the primary key for `A` – which does not work atm.
Either you remove the PHP typehint on the property `b` and add it back via phpdoc.
Or you have to add a new primary key column that you generate out of the FK to `B` and remove the ID-Annotation from `A::b`.
• via github.com
Dominik Schwind
Yeah, the second option sounds a lot more future proof for now. Thanks!
• via github.com