I have the following entity graph:
#[ORM\Entity]
class Professional
{
#[ORM\Id]
#[ORM\Column(type: 'professional_id')]
#[ORM\GeneratedValue(strategy: 'NONE')]
private ProfessionalId $id;
#[ORM\OneToOne(targetEntity: ProfessionalCatchmentArea::class, mappedBy: 'professional', cascade: ['all'])]
private ?ProfessionalCatchmentArea $catchmentArea = null;
}
#[ORM\Entity]
class ProfessionalCatchmentArea
{
#[ORM\Id]
#[ORM\OneToOne(targetEntity: Professional::class, inversedBy: 'catchmentArea')]
#[ORM\JoinColumn(nullable: false)]
private Professional $professional;
#[ORM\OneToMany(targetEntity: ProfessionalCatchmentAreaZone::class, mappedBy: 'catchmentArea', orphanRemoval: true, cascade: ['all'])]
private Collection $zones;
}
#[ORM\Entity]
class ProfessionalCatchmentAreaZone
{
#[ORM\Id]
#[ORM\Column(type: 'uuid')]
#[ORM\GeneratedValue(strategy: 'NONE')]
private Uuid $id;
#[ORM\ManyToOne(targetEntity: ProfessionalCatchmentArea::class, inversedBy: 'zones')]
#[ORM\JoinColumn(name: 'professional_id', referencedColumnName: 'professional_id', nullable: false)]
private ProfessionalCatchmentArea $catchmentArea;
}
Note: ProfessionalId
is a custom ID class containing a UUID.
As you can see, Professional
has a one-to-one relationship with ProfessionalCatchmentArea
, which in turn has a one-to-many relationship with ProfessionalCatchmentAreaZone
.
Due to the one-to-one, ProfessionalCatchmentArea
shares its primary key with Professional
.
Therefore, the JoinColumn
for the many-to-one relationship from ProfessionalCatchmentAreaZone
to ProfessionalCatchmentArea
is based on the professional_id
column.
For some reason, even though Doctrine does not complain with this mapping (bin/console doctrine:schema:validate
is OK), and even though traversal from Professional
to $catchmentArea
to $zones
works fine, attempting to load the ProfessionalCatchmentAreaZone
entities directly from the EntityRepository
fails:
$entityManager->getRepository(ProfessionalCatchmentAreaZone::class)->findAll();
Cannot assign
App\Entity\ProfessionalId
to propertyApp\Entity\ProfessionalCatchmentArea::$professional
of typeApp\Entity\Professional
Any idea why?
Is this mapping one-to-one => one-to-many, while sharing the primary key in the one-to-one relationship, not supported? Is this a Doctrine bug?