Doctrine 使用列聚合扩展扩展模型
技术: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将这些列添加到实体类中,因为所有内容基本上都存储在同一个表中。这意味着这些列也可供公司条目使用,但也许您可以禁止在那里使用它们。
但是,您可以使用不同的表并使用外键引用它们。这将为您提供如下布局:
这样你就可以总是(懒惰地)获取有关你的实体的附加信息,并且表本身仍然很小。如果您将实体实现为列聚合,则 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:
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.