2011-12-09 96 views
2

帶班表繼承類型信息,類表繼承映射在原則2:如何從實體

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

我怎樣才能從實體類型的信息?由於discr是數據庫列,但不是實體的屬性?

回答

4

您可以創建在父類的抽象方法getKind()並實現它的子類像

public function getKind() 
{ 
    return 'employee'; 
} 
+0

其實我有一個'$ type'屬性,它在構造繼承類設置。我注意到從數據庫/實體管理器中檢索時,構造函數不會被調用?我會用你的方法代替 –

+0

學說不會調用構造函數http://stackoverflow.com/questions/6555237/how-does-doctrine-2-retrieve-entities-without-calling-the-entitys-constructor – Koc