I have a table person
with id
as identifier column and a tables student
with person_id
as PRIMARY KEY
. The column student.person_id
references (FOREIGN KEY
) the person.id
.
There are accordingly three classes: An abstract
class Person
with a member id
and its sub-classe Student
without any "identifying property".
I'm trying to define the Class Table Inheritance
for this structure:
Person
use Doctrine\ORM\Mapping as ORM;
/**
* Parent
*
* @ORM\Table(
* name="person"
* ),
* @ORM\Entity
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="type", type="string")
*/
abstract class Person
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
}
Student
use Doctrine\ORM\Mapping as ORM;
/**
* Student
*
* @ORM\Table(name="student")
* @ORM\Entity
*/
class Student extends Person
{
}
But when I try to retrieve the objects
$testRepo = $entityManager->getRepository(Student::class);
$entities = $testRepo->findAll();
I'm getting an exception. The Problem is, that Doctrine assumes, the identifying column for the tables students
to have the name id
and build a SELECT
statement like this:
SELECT t1.id AS id_2,
t1.... AS ..._3,
...
FROM student t0
INNER JOIN `person` t1 ON t0.id = t1.id
instead of
SELECT t1.id AS id_2,
t1.... AS ..._3,
...
FROM student t0
INNER JOIN `person` t1 ON t0.person_id = t1.id
and runs into the error
Error Code: 1054
Unknown column 't0.id' in 'on clause'
For a new system the simplest and cleanest way might be to rename the student.person_id
column. But I'm currently migrating an existing system to Doctrine 2 and would like to let so much as possible as it is, so long the migration is not yet completed.
How to define a Class Table Inheritance in Doctrine 2 by using different column names for the identifier of the parent and the identifier of the child table(-s)?