S
S
Sergey Kulikov2016-01-28 12:47:54
symfony
Sergey Kulikov, 2016-01-28 12:47:54

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'.

or 2) Call somehow EntityManager from Entity and get the required field in the usual way, but I also didn’t find any working way to do this (it doesn’t allow overriding the Symfony2 constructor)
Help me solve the problem

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Grachev, 2016-01-29
@Preemiere

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 question

Ask a Question

731 491 924 answers to any question