Implementing Class Table Inheritance(CTI) across multiple database tables in Doctrine 2

872 views Asked by At

Need some help please?

I have 2 classes, a Parent class, say Person and a Child say Employee using Doctrine 2's class table inheritance mapping strategy. The classes' corresponding tables exist in separate database tables: Person table exists in database: dbOne and Employee table is in dbTwo.

The classes are like:

/**
 * @Entity
 * @InheritanceType("JOINED")
 * @DiscriminatorColumn(name="discr", type="string")
 * @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
 * @Table(name="Person")
 */
class Person
{

and the child class

/**
 * @Entity
 * @Table(name="Employee")
 */
class Employee extends Person
{

The schema for the Employee table looks like so:

CREATE TABLE Employee (
    id INT NOT NULL,
    department VARCHAR(50) NOT NULL,
    PRIMARY KEY(id)
) ENGINE = InnoDB;
ALTER TABLE Employee ADD FOREIGN KEY (id) REFERENCES **dbOne.Person**(id) ON DELETE CASCADE

There are two problems actually.

  1. Since Doctrine maps one entity manager per database, if I call

$this->entityManager->flush();

On Person object, it flags an error:

Message: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'dbOne.Employee' doesn't exist 

The reverse is true if I call the entity manager (for dbTwo) to save an Employee object. The problem is absent when the tables are in the same database. How can I get around this issue please? Also, how can I retrieve the SQL query the entity manager uses during the flush() operation? It would be helpful to know what SQL is generated and used - like:

$this->entityManager->getQuery();//obviously this is just to explain what I mean

  1. The second problem is getting Doctrine to insert the right value in the discr column of the database table: Person according to the @DiscriminatorMap details set. This works well when the value is Parent instance (i.e. Person) but not the child instance (i.e. Employee). Thanks for helping out.
0

There are 0 answers