代码之家  ›  专栏  ›  技术社区  ›  Rene Terstegen

使用列聚合扩展模型

  •  0
  • Rene Terstegen  · 技术社区  · 16 年前

    技术:ORM,条令1.1.6,KohanaPHP

    第1.1.6条。如何在不同的桌子上分布模型?

    具体情况:

    我有一个包含ID、登录名和密码的类实体,它有一个emailaddress、多个地址和一些其他关系。我还有另外两个类,Company和Person,它们扩展了实体。我想用 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,
                 ));
        }
    }

    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 回复  |  直到 16 年前
        1
  •  1
  •   Thomas Schaub    16 年前

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

    1. 实体 -存储所有实体通用的基本信息。此外,还将此实体的类型(用户、公司)存储为id。
    2. -存储特定于用户的信息和对应实体的密钥。
    3. ,如果没有其他信息,则可能几乎为空(根据实现此解决方案的方式,为了简单起见,仍然可以添加一行仅包含实体id)

    这样,您就可以一直(懒散地)获取有关实体的附加信息,并且表本身仍然很小。如果您将实体实现为列聚合,则原则将负责返回正确的对象。然后可以添加自定义函数来获取附加信息。