2011-08-23 53 views
2

私人性质,而阅读Doctrine documentation,我发现这个例子:查询父的原则

/** 
* @Entity 
* @InheritanceType("SINGLE_TABLE") 
* @DiscriminatorColumn(name="discr", type="string") 
* @DiscriminatorMap({"person" = "Person", "employee" = "Employee"}) 
*/ 
class Person 
{ 
    /** 
    * @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; 
} 

根据该文档时,Employee类可以查询这样:

SELECT e FROM Entities\Employee e WHERE e.name = 'test'; 

我的问题是:如果PersonEmployee都具有name属性,具有不同的范围和值,那该怎么办?例如:

class Person { 
    private $name; 
} 

class Employee extends Person { 
    private $name; 
} 

我的猜测是,这个查询将针对Employee::$name跑:

SELECT e FROM Entities\Employee e WHERE e.name = 'test'; 

是否有可能的话,对查询Person::$name,但仍只返回Employee实例?喜欢的东西:

SELECT e FROM Entities\Employee e WHERE e.parent.name = 'test'; 

回答

0

教义实际上,你可以按层次结构只能有一个独特的属性名称,它会在实体使用,通过这种方式使覆盖类的属性是不是一个好主意。

在这个例子中有不同的值的唯一方法是定义不同的属性名称和数据库字段:

class Person 
{ 
    // ... 

    /** 
    * @ORM\Column(type="string", length=50) 
    */ 
    protected $name; 
} 

class Employee extends Person 
{ 
    // ... 

    /** 
    * @ORM\Column(type="string", length=50, name="employeeName") 
    */ 
    protected $employeeName; 
} 
+0

谢谢,确实是有道理的DQL这样! – Benjamin

+1

对于任何感兴趣的人,这实际上记录在[这里](http://www.doctrine-project.org/docs/orm/2.1/en/reference/architecture.html):“类层次结构中的任何两个实体类从彼此直接或间接继承必须不具有相同名称的映射属性,也就是说,如果B从A继承,则B必须不具有与从A继承的已映射字段具有相同名称的映射字段。 – Benjamin