Answer the question
In order to leave comments, you need to log in
How to link 2 Entity in Doctrine2 not by primary key?
There is a database which cannot be changed. And it has a users table with users and their passwords, and a userprofile table with their names and stuff. They are connected (for some reason) not by the primary key, but by email. It is necessary to get the value of the name field from the userprofile table in the Entity User. The base was not designed by me and it is used for other applications - so I cannot change it. I came to the conclusion that you need to either:
1) Add a profile_name column and write links between the two Entities. I did it like this:
/**
* @ORM\OneToOne(targetEntity="UserProfile")
* @ORM\JoinColumn(name="name", referencedColumnName="email")
*/
private $profile_name;
But symfony2 throws an error when executed
The referenced column name 'email' has to be a primary key column on the target entity class 'AppBundle\Entity\UserProfile'.
Answer the question
In order to leave comments, you need to log in
Every entity class must have an identifier/primary key.
docs.doctrine-project.org/en/latest/reference/basi...
28.1.1. Join-Columns with non-primary keys
It is not possible to use join columns pointing to non-primary keys.
doctrine-orm.readthedocs.org/projects/doctrine-orm...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question