Discriminator problems with Doctrine 2
I am implementing inheritance mapping with D2 using Class Table Inheritance strategy. I have a parent class called Person with the following code block
namespace Zain\Entity;
/**
* @Entity
* @InheritanceType("JOINED")
* @DiscriminatorColumn(name="Specialty", type="string") // what other types exist?
* @DiscriminatorMap({"person" = "\Zain\Person", "employee" = "\Staff\Entities\Employee"})
*
* @Table(name="db_One.tblPerson")
*
*/
class Person
{
...
And I have a child class named Employee with the following code:
namespace Staff\Entities;
开发者_运维知识库/**
* Description of Employee
* @Entity
* @Table(name="db_Two.tblEmployee")
*
*/
class Employee extends \Zain\Entity\Person
{
...
The MySQL table: tblPerson has a Discriminator Column called Specialty defined as:
`Specialty` varchar(45) NOT NULL
The problem occurs when I have an instance of Employee and attempt to persist it.
When the Employee instance is persisted, I expect that the object name, employee (string) would be saved in the Specialty column of table Person. However this has not happened. I encounter an error message:
Message: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'specialty' cannot be null
I understand that this error means that no value is being passed for the Specialty column in the SQL statement generated and used by the EntityManager. And I have set a Not Null constraint on the tblPerson > Specialty Column.
If I removed the not null constraint, I can get this to work - but that defeats the purpose. In helping out with this, could you please show me how to retrieve the generated SQL statement used/to be used by the entity manager during a persist call?
It would be great to have a good end to a couple of days' search for answers. Thanks again.
Doctrine supports a variety of types. The main used ones in DiscriminatorMap are string and integer (and its derivates: bigint, smallint, float, ...).
Well, the issue to me seem to be the class name you're referring to. String representations of class name always point to root namespace, so including the "\" in the beginning only causes you troubles. I'd suggest you to remove it and check if it works.
精彩评论