Querying superclass' private properties in Doctrine
While reading the Doctrine documentation, I found this example:
/**
* @Entity
* @InheritanceType("SINGLE_TABLE")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
*/
class Person
{
/*开发者_如何学JAVA*
* @Id @Column(type="integer")
* @GeneratedValue
*/
protected $id;
/**
* @Column(type="string", length=50)
*/
protected $name;
}
/**
* @Entity
*/
class Employee extends Person
{
/**
* @Column(type="string", length=50)
*/
private $department;
}
According to the doc, the Employee
class can be queried this way:
SELECT e FROM Entities\Employee e WHERE e.name = 'test';
My question is: what if Person
and Employee
both had a name
property, with different scopes and values? For example:
class Person {
private $name;
}
class Employee extends Person {
private $name;
}
My guess is that this query would be ran against Employee::$name
:
SELECT e FROM Entities\Employee e WHERE e.name = 'test';
Would it be possible then, to query against Person::$name
, while still returning only Employee instances? Something like:
SELECT e FROM Entities\Employee e WHERE e.parent.name = 'test';
With Doctrine you actually can have only one unique property name per hierarchy, which will be used in Entity, so overriding class properties in this way is not a good idea.
The only way to have different values in this example is to define different property names and database fields:
class Person
{
// ...
/**
* @ORM\Column(type="string", length=50)
*/
protected $name;
}
class Employee extends Person
{
// ...
/**
* @ORM\Column(type="string", length=50, name="employeeName")
*/
protected $employeeName;
}
精彩评论