Doctrine 2 mapping that references a unique key

The main question:

Is it possible to map the association using the Doctrine link, not a primary key, but only a unique key?

Extended version:

I have an Entity ( Participation ) that can reference 2 other objects ( DropoutCause and DischargeType ). Depending on this combination, some other attributes are implied based on another (4th) table ( DropoutScenario ) in the database. Since either of the two reference objects can be empty, I could not declare them primary, but only a unique key in the 4th table.

The problem is that I get an error when trying to map this to Doctrine:

Missing value for primary key ID Application \ Entity \ Training \ DropoutScenario

Am I doing something wrong, or is it simply not possible with the Doctrine? If not, is there a better solution, how can I do this?

I searched for quite some time and dug the Doctrine documentation, but I just couldn't find anything in this ...

Cut out code samples of my comparisons below.

Participation:

 <?php namespace Application\Entity\Trainings; use Doctrine\ORM\Mapping as ORM; /** * @ORM\MappedSuperclass */ abstract class Participation { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\ManyToOne(targetEntity="Application\Entity\DropoutCause") * @ORM\JoinColumn(name="dropout_cause_id", referencedColumnName="id")) */ protected $dropoutCause; /** * @ORM\ManyToOne(targetEntity="Application\Entity\DischargeType") * @ORM\JoinColumn(name="discharge_id", referencedColumnName="id")) */ protected $dischargeType; /** * @ORM\ManyToOne(targetEntity="DropoutScenario") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="discharge_id", referencedColumnName="discharge_id"), * @ORM\JoinColumn(name="dropout_cause_id", referencedColumnName="dropout_cause_id") * }) */ private $scenario; 

DropoutScenario:

 <?php namespace Application\Entity\Trainings; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="training_dropout_scenarios") */ class DropoutScenario { /** * @var int * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\ManyToOne(targetEntity="Application\Entity\DropoutCause") * @ORM\JoinColumn(name="dropout_cause_id", referencedColumnName="id")) */ protected $dropoutCause; /** * @ORM\ManyToOne(targetEntity="Application\Entity\DischargeType") * @ORM\JoinColumn(name="discharge_id", referencedColumnName="id")) */ protected $dischargeType; /** @ORM\Column(type="integer", name="dropout_cause_id") */ protected $dropoutCauseId; /** @ORM\Column(type="integer", name="discharge_id") */ protected $dischargeTypeId; 
+8
source share
2 answers

Since I received no answer, I did another study today and found the answer to my question on the Doctrine mailing list forum. Looks like I just searched for the wrong keywords ...

Doctrine 2, unfortunately, does not support this. What a pity! :(

From the teachings documentation: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/limitations-and-known-issues.html#join-columns-with-non-primary-keys

Cannot use join columns pointing to non-primary keys. The doctrine will think that these are primary keys and creates a lazy proxy load with data that can lead to unexpected results. the doctrine may, for performance reasons, not validate this setting at run time, but only with the Validate Schema command.

A similar question: Is it possible to refer to a column other than & # 39; id & # 39; for JoinColumn?

+6
source

As in the following links, Doctrine does not support this type of display.

http://www.doctrine-project.org/jira/browse/DDC-1269

http://www.doctrine-project.org/jira/browse/DDC-1114

I think this will help you.

0
source

Source: https://habr.com/ru/post/970387/


All Articles