2010-08-10 32 views
1

以下情況:是否可以使用Doctrine 2擴展模型?

家長:

namespace Base; 

/** @Entity @Table(name="section") */ 
class Section extends Skeleton { 
/** 
* @Id @Column(type="integer") 
* @GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 

/** @Column(length=256) */ 
protected $title; 

/** @Column(length=256) */ 
protected $stylesheet; 
} 

兒童:

namespace Base2; 

use \Base\Section AS BaseSection; 

/** @Entity @Table(name="tbl_section") */ 
class Section extends BaseSection { 
/** 
* @Id @Column(type="integer") 
* @GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 

/** @Column(length=256) */ 
protected $title; 

/** @Column(length=256) */ 
protected $stylesheet; 
} 

當我試圖從數據庫中檢索的部分,它拋出一個錯誤:

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 't0.id' 
in 'where clause' in /var/www/eage_new_zf/library/Doctrine/DBAL/Connection.php 
on line 567 Call Stack #TimeMemoryFunctionLocation 10.0004489704{main}( 
)../index.php:0 20.03193296632Zend_Controller_Front->dispatch(???, ??? 
)../index.php:27 30.04574505172Zend_Controller_Dispatcher_Standard->dispatch( 
object(Zend_Controller_Request_Http)[39], object(Zend_Controller_Response_Http)[40] 
)../Front.php:954 Variables in local scope (#3) 

的查詢它試圖執行的是:

SELECT 
    t1.id AS id2, 
    t1.title AS title3, 
    t1.stylesheet AS stylesheet4 
FROM 
    tbl_section t1 
WHERE 
    t0.id = ? 

t0沒有被定義,所以在技術上它是正確的我得到一個錯誤。但如何解決這個問題?這是教義2中的錯誤嗎?或者我做錯了什麼。

回答

2

您可以使用單表或連接表繼承。區別在於使用一個具有可空列的表,或者根據子類使用多個表。請參閱用戶手冊MROE信息:

http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/inheritance-mapping.html

在你的情況最頂端/根類(基地\段)

/** 
* @Entity @Table(name="section") 
* @InheritanceType("SINGLE_TABLE") 
* @DiscriminatorColumn(name="discr", type="string") 
* @DiscriminatorMap({"base" = "Base\Section", "child" = "Base2\Section"}) 
*/ 

命名類是一個不好的做法,順便說一句,您應該爲您的類關於他們在做什麼實施明智。即使它複製已包含在名稱空間中的單詞,即您的示例中的Base \ BaseSection和BAse2 \ Base2Section。

+0

感謝您的回答。我完全錯過了你提到的文檔,但我想這會做。 我不知道在最後一部分我是否同意你的看法,但這僅僅是我個人的想法。無論如何感謝您的建議。 – 2010-08-11 07:07:17

+0

鏈接已死,這是一個新的:http://doctrine-orm.readthedocs.org/en/latest/reference/inheritance-mapping.html;) – 2015-02-21 03:31:39