2

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)?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
automatix
  • 14,018
  • 26
  • 105
  • 230
  • Please put solutions (even if it is only a work-around) in the *answers section*, not in the question. It was correctly removed before, please leave it out. You are free to post it as an answer below. – Martijn Pieters Sep 27 '17 at 17:15

1 Answers1

-6

(Posted solution on behalf of the OP).

Since I urgently needed a solution for this problem, I resolved it (or better: went around) by renaming of the identifier column to id.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • It's not a solution. Please remove this. – automatix Aug 19 '17 at 20:08
  • @automatix: Your edit to your question indicated that the problem was resolved, and now you have restored it, people will see that and believe that it is resolved to your satisfaction. I recommend you change that update to say that you have a partial solution ("see one of the answers") but that you are seeking something else (and perhaps you could define what that better solution would look like, or why your workaround is not ideal). – halfer Aug 19 '17 at 20:12
  • @automatix: it's a work-around *you posted*. It could be a solution that helps others, it belongs in the answers section. Having it posted here does *not* preclude others from posting a better solution. – Martijn Pieters Sep 27 '17 at 18:03
  • Thanks for your edits, @AFriend. I've rolled this one back, since it is part of the posting guidelines to attribute material where the poster is not the author. – halfer Dec 27 '18 at 08:37
  • @Afriend: my interpretation of your edits is that they are retaliatory. I do not advise you get into that habit. I am repairing the work of a few hundred authors, so I can assure you I am not targetting you, and I can give you some confidence that we prefer material to be written with some brevity here. A few of your edits on my material were rejected by the community (probably for being too minor). I have rolled back two and accepted two. I am happy to accept good quality edits to my material when they are made in good faith. – halfer Dec 27 '18 at 08:48