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

Orm 使用列聚合扩展扩展模型,orm,doctrine,models,multiple-tables,Orm,Doctrine,Models,Multiple Tables,技术:ORM,条令1.1.6,科纳帕 第1.1.6条。如何在不同的表上分布模型 详细情况: 我有一个包含ID、登录名和密码的类实体,它有一个emailaddress、多个Address和一些其他关系。我还有另外两个类,Company和Person,它们扩展了实体。我想使用扩展它们,以便将所有登录和密码信息保存在一个地方。现在我想向Person类添加特定的列(firstname、lastname等),但我找不到如何做到这一点。文档给出的唯一示例是没有额外列的示例 当前类别 实体类: class E

技术:ORM,条令1.1.6,科纳帕

第1.1.6条。如何在不同的表上分布模型

详细情况:

我有一个包含ID、登录名和密码的类实体,它有一个emailaddress、多个Address和一些其他关系。我还有另外两个类,Company和Person,它们扩展了实体。我想使用扩展它们,以便将所有登录和密码信息保存在一个地方。现在我想向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) )); } } 类实体扩展了DOCU记录 { 公共函数setTableDefinition(){ $this->setTableName('entity'); $this->hasColumn('id','integer',4,数组( '类型'=>'整数', “长度”=>4, “未签名”=>0, “primary”=>true, “自动增量”=>true, )); $this->hasColumn('login','string',64,数组( '类型'=>'字符串', “长度”=>64, “已修复”=>错误, “primary”=>false, “notnull”=>true, “自动增量”=>false, )); $this->hasColumn('password','string',64,数组( '类型'=>'字符串', “长度”=>64, “已修复”=>错误, “primary”=>false, “notnull”=>true, “自动增量”=>false, )); $this->hasColumn('created','date',null,array( '类型'=>'日期', “primary”=>false, “notnull”=>false, “自动增量”=>false, )); $this->hasColumn('modified','date',null,数组( '类型'=>'日期', “primary”=>false, “notnull”=>false, “自动增量”=>false, )); $this->setsubclass(数组( “Person”=>数组(“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, )); } } 类人扩展实体 { 公共函数setTableDefinition(){ $this->setTableName('person'); $this->hasColumn('id','integer',4,数组( '类型'=>'整数', “长度”=>4, “未签名”=>0, “primary”=>true, “自动增量”=>true, )); $this->hasColumn('firstname','string',255,数组( '类型'=>'字符串', “长度”=>255, “已修复”=>错误, “primary”=>false, “notnull”=>true, “自动增量”=>false, )); $this->hasColumn('插入','字符串',64,数组( '类型'=>'字符串', “长度”=>64, “已修复”=>错误, “primary”=>false, “notnull”=>false, “自动增量”=>false, )); $this->hasColumn('lastname','string',255,数组( '类型'=>'字符串', “长度”=>255, “已修复”=>错误, “primary”=>false, “notnull”=>true, “自动增量”=>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 创建表“person”( `id`INT自动递增, `firstname`VARCHAR(255)不为空, `插入'VARCHAR(64), `lastname`VARCHAR(255)不为空, 主键(`id`) )引擎=INNODB 创建表“实体”(` id`INT自动递增, `登录名'VARCHAR(64)不为空, `密码'VARCHAR(64)不为空, `创建日期, `修改日期, 主键(`id`) )引擎=INNODB
有人能告诉我如何做到这一点吗

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

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

  • 实体-存储所有实体通用的基本信息。此外,您还将此实体的类型(用户、公司)存储为id
  • 实体类型-存储每个实体类型的Core对应表
  • 用户-存储特定于用户的信息和对应实体的密钥
  • 公司-与用户相同,如果没有其他信息,可能几乎为空(取决于您实现此解决方案的方式,为了简单起见,您仍然可以添加一行仅包含实体id)
  • 通过这种方式,您可以始终(懒惰地)获取有关实体的附加信息,并且表本身保持苗条。如果将实体实现为列聚合,则原则将负责返回正确的对象。然后,您可以添加用于获取附加信息的自定义函数


    你可以在第二节中省去间接性。

    @Larry\u Croft:所以基本上我想要的是不可能的?我必须将我的个人和公司信息放在一个表中?是的,至少要使用列聚合。我将在我的答案中添加一个替代解决方案。谢谢。你建议我的旧情况(无意冒犯,你不可能知道)。我想我会努力扩展你/我/我们的想法。再次感谢你的帮助!