Doctrine使用列聚合扩展扩展模型

时间:2010-02-23 16:04:19

标签: orm doctrine models multiple-tables

技术:ORM,Doctrine 1.1.6,KohanaPHP

使用Doctrine 1.1.6。如何在不同的表格上传播模型?

详细情况:

我有类Entity,其中包含ID,登录名和密码,并且有一个电子邮件地址,许多地址和其他一些关系。我还有另外两个类,Company和Person,它们扩展了Entity。我想使用Column aggregation扩展它们,以便将所有登录和密码信息保存在一个地方。现在我想在Person类(firstname,lastname等)中添加特定的列,但是我找不到如何执行此操作。文档提供的唯一示例是没有额外列的示例。

当前班级

实体类:

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)
            ));
    }
}

人类:

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 个答案:

答案 0 :(得分:1)

您必须将这些列添加到实体类,因为所有内容基本上都存储在同一个表中。这意味着这些列也可供公司条目使用,但也许您可以禁止在那里使用它们。

但是,您可以使用不同的表并使用外键引用它们。这将为您提供如下布局:

  1. entity - 存储所有实体共有的基本信息。此外,您将此实体的类型(用户,公司)存储为ID。
  2. entity_types - 为每个实体类型存储coreesponding表
  3. 用户 - 存储特定于用户的信息和相应实体的密钥。
  4. 公司 - 与用户相同,如果没有其他信息,可能几乎为空(取决于您如何实施此解决方案,您仍然可以添加一行只包含简单的实体ID)
  5. 通过这种方式,您可以随时(懒惰)获取有关您的实体的其他信息,并且表格本身仍然很小。如果您将实体视为列聚合,则Doctrine将负责返回正确的对象。然后,您可以添加自定义函数以获取其他信息。

    你可以在2中省略间接。