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

发布于 2024-08-22 20:11:55 字数 3760 浏览 4 评论 0原文

技术:ORM、Doctrine 1.1.6、KohanaPHP

以及 Doctrine 1.1.6。如何将模型分布在不同的表上?

详细情况:

我有一个实体类,其中包含ID、登录名和密码,并且有一个电子邮件地址、多个地址和一些其他关系。我还有另外两个类,Company 和 Person,它们扩展了 Entity。我想使用 列聚合 来扩展它们因此所有登录名和密码信息都保存在一个地方。现在我想向我的 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

有人可以告诉我如何完成此操作吗?

Techniques: ORM, Doctrine 1.1.6, KohanaPHP

With Doctrine 1.1.6. How do I spread a model over different tables?

Detailed situation:

I have the class Entity which contains an ID, login and password and has one emailaddress, many addresses and some other relations. I have two other classes, Company and Person, which extend Entity. I want to extend them using Column aggregation so all login and password information is saved in one place. Now I want to add specific columns to my Person class (firstname, lastname, etc), but I can't find how to do this. The only example the documentation gives is one without extra columns.

Current classes

Entity class:

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:

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 generated:

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

Can somebody tell me how to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老旧海报 2024-08-29 20:11:55

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

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

  1. 实体 - 存储所有实体共有的基本信息。此外,您将该实体的类型(用户、公司)存储为 ID。
  2. entity_types - 存储每个实体类型的核心对应表
  3. User - 存储特定于用户的信息和相应实体的密钥。
  4. 公司 - 与用户相同,如果没有附加信息,可能几乎为空(取决于您如何实现此解决方案,您仍然可以添加一行仅包含实体 ID为了简单起见)

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

您可以省略 2 中的间接寻址。

You'll have to add these columns to the entity class since everything is basically stored in the same table. That means that these columns will be available to the company entries too, but maybe you can forbid using them there.

You can however use different tables and reference them with a foreign key. This will give you a layout like this:

  1. entity - stores basic information common to all entities. Furthermore you store the type of this entity (User, Company) as an id.
  2. entity_types - stores the coreesponding table for each entity type
  3. User - stores information specific to the users and a key to the corresponding entity.
  4. Company - same as User, may be nearly empty if there is no additional info (depending on how you implement this solution, you can still add one row just containing the entity id for simplicity)

This way you can alway (lazy) fetch additional information about your entities and the table itself remains slim. If you realize entity as an column aggregation Doctrine will take care of returning the right object. Then you can add your custom functions for fetching the additional information.

You can leave out the indirection in 2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文