In this example:
we see that
SELECT e FROM Entities\Employee e WHERE e.name = 'test'
makes this query:
SELECT p0_.id AS id0, p0_.name AS name1, p0_.department AS department2,
p0_.discr AS discr3 FROM Person p0_ WHERE (p0_.name = ?) AND p0_.discr IN ('employee')
Is there a way to query records only with the discriminator "Person"?
This:
SELECT e FROM Entities\Person e WHERE e.name = 'test'
returns all the records.
Any ideas?
By default the parent entity in a Inheritance Mapped Entity does not seem to use the Discriminator rules.
I got round this by creating a base class/entity.
/**
* @Entity
* @InheritanceType("SINGLE_TABLE")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
*/
class PersonBase
{
/**
* @Id @Column(type="integer")
* @GeneratedValue
*/
protected $id;
/**
* @Column(type="string", length=50)
*/
protected $name;
// ...
}
/**
* @Entity
*/
class Person extends PersonBase
{
}
/**
* @Entity
*/
class Employee extends PersonBase
{
/**
* @Column(type="string", length=50)
*/
private $department;
// ...
}
Now Person will use discriminator rules, you need never directly reference PersonBase.