2010-02-23 13 views
0

技術擴展擴展模型:ORM,學說1.1.6,KohanaPHP主義用柱聚集

教義1.1.6。我如何在不同的表格上傳播模型?

詳細情況:

我有實體類包含一個ID,登錄名和密碼,並有一個EMAILADDRESS,許多地址和其他一些關係。我有兩個其他類,公司和個人,擴展實體。我想用Column aggregation來擴展它們,所以所有的登錄和密碼信息都保存在一個地方。現在我想添加特定的列到我的Person類(名字,姓氏等),但我找不到如何做到這一點。文檔給出的唯一例子是沒有額外列的例子。

當前類

實體類:

class Entity extends Doctrine_Record 
{ 
    public function setTableDefinition() { 
     $this->setTableName('entity'); 
     $this->hasColumn('id', 'integer', 4, array(
      'type' => 'integer', 
      'length' => 4, 
      'unsigned' => 0, 
      'primary' => true, 
      'autoincrement' => true, 
      )); 
     $this->hasColumn('login', 'string', 64, array(
      'type' => 'string', 
      'length' => 64, 
      'fixed' => false, 
      'primary' => false, 
      'notnull' => true, 
      'autoincrement' => false, 
      )); 
     $this->hasColumn('password', 'string', 64, array(
      'type' => 'string', 
      'length' => 64, 
      'fixed' => false, 
      'primary' => false, 
      'notnull' => true, 
      'autoincrement' => false, 
      )); 
     $this->hasColumn('created', 'date', null, array(
      'type' => 'date', 
      'primary' => false, 
      'notnull' => false, 
      'autoincrement' => false, 
      )); 
     $this->hasColumn('modified', 'date', null, array(
      'type' => 'date', 
      'primary' => false, 
      'notnull' => false, 
      'autoincrement' => false, 
      )); 

     $this->setSubclasses(array(
       'Person' => array("type" => 1) 
      )); 
    } 
}

Person類:

class Person extends Entity 
{ 
    public function setTableDefinition() { 
     $this->setTableName('person'); 
     $this->hasColumn('id', 'integer', 4, array(
      'type' => 'integer', 
      'length' => 4, 
      'unsigned' => 0, 
      'primary' => true, 
      'autoincrement' => true, 
      )); 
     $this->hasColumn('firstname', 'string', 255, array(
      'type' => 'string', 
      'length' => 255, 
      'fixed' => false, 
      'primary' => false, 
      'notnull' => true, 
      'autoincrement' => false, 
      )); 
     $this->hasColumn('insertion', 'string', 64, array(
      'type' => 'string', 
      'length' => 64, 
      'fixed' => false, 
      'primary' => false, 
      'notnull' => false, 
      'autoincrement' => false, 
      )); 
     $this->hasColumn('lastname', 'string', 255, array(
      'type' => 'string', 
      'length' => 255, 
      'fixed' => false, 
      'primary' => false, 
      'notnull' => true, 
      'autoincrement' => false, 
      )); 
    } 
}

SQL生成:

CREATE TABLE `person` (
    `id` INT AUTO_INCREMENT, 
    `firstname` VARCHAR(255) NOT NULL, 
    `insertion` VARCHAR(64), 
    `lastname` VARCHAR(255) NOT NULL, 
    PRIMARY KEY(`id`) 
) ENGINE = INNODB 

CREATE TABLE `entity` (` 
    id` INT AUTO_INCREMENT, 
    `login` VARCHAR(64) NOT NULL, 
    `password` VARCHAR(64) NOT NULL, 
    `created` DATE, 
    `modified` DATE, 
    PRIMARY KEY(`id`) 
) ENGINE = INNODB

燦有人告訴我如何做到這一點?

回答

1

您必須將這些列添加到實體類,因爲一切基本上都存儲在同一個表中。這意味着這些列也可用於公司參賽作品,但也許你可以在那裏禁止使用它們。

但是,您可以使用不同的表並使用外鍵引用它們。這會給你這樣的佈局:

  1. 實體 - 商店共同所有實體的基本信息。此外,您將此實體(用戶,公司)的類型存儲爲ID。
  2. entity_types - 存儲每個實體類型
  3. 用戶的coreesponding表 - 特定於用戶和相應的實體密鑰存儲信息。
  4. 公司 - 同用戶,可能是幾乎是空的,如果沒有其他信息(具體取決於您如何實施該解決方案,你仍然可以添加一行只包含簡單的實體ID)

通過這種方式,您可以總是(懶惰地)獲取有關實體的附加信息,並且表本身仍然很小。如果您意識到實體是一個列聚合,則原則將負責返回正確的對象。然後,您可以添加自定義函數以獲取附加信息。

你可以忽略間接2.

+0

@Larry_Croft:所以基本上我想要的是不可能的?我必須將我的人員和公司信息放在一張表中? – 2010-02-25 07:27:59

+0

是的,至少有列聚合。我會爲我的答案添加一個替代解決方案。 – 2010-02-25 09:45:33

+0

謝謝。你建議我的舊情況(沒有冒犯,你不知道)。我想我會試着延長你的/我的/我們的想法。再次感謝你的幫助! – 2010-02-25 10:21:57