Doctrine2设计遗产问题 [英] Doctrine2 designing problems with inheritance
问题描述
我目前正在设立一个新的应用程序,并决定使用doctrine2作为一个orm。
我是新手上学的教程2,让我的设计让我想要的方式有一些困难。
这是我想要做的: / p>
我有以下课程:
class User {
protected $ userid;
protected $ role;
protected $ lang;
protected $ email;
protected $ disabled;
...
+ getter和setter
}
class Person extends User {
protected $ personid;
protected $ name;
protected $ firstname;
protected $ company;
protected $ functiontitle
...
+ getter和setter
}
类候选扩展用户{
protected $ candidateid;
protected $ name;
protected $ firstname;
protected $ title;
protected $ birthdate;
...
+ getter和setter
}
在postgresql数据库中,我目前有一个类似的设置,3个表,每个他们的特定列。
现在我想使用yaml映射,并希望所有3个类可以作为实体访问。
我也不想在我的数据库中复制很多字段。
我该如何做到这一点?
我一直在寻找解决方案几天,但没有提出任何工作。
如果我想要做的是不可能的,那么请说明我的选择是什么,为什么不支持这个选择。
没有一个ORM,似乎我可以轻松地添加外键到候选人和个人加入用户表,但我不能得到这样的使用原则。
这是我目前必须将其设置为类表继承的yaml文件:
用户:
MyProject\model\User:
类型:实体
表:`user`
id:
userid:
列:userid
类型:整数
可空:false
注释:''
id:true
字段:
角色:
列:角色
类型:字符串
nullable:true
length:null
fixed:false
comment:''
电子邮件:
类型:string
可空:true
length:null
fixed:false
comment:''
禁用:
类型:boolean
可空:true
注释:''
继承类型:JOINED
DiscriminatorColumn:
名称:discr
类型: string
DiscriminatorMap:
person:Person
候选人:Candidate
lifecycleCallbacks:{}
个人:
MyProject\model\Person:
type:entity
表:person
字段:
名称:
类型:string
可空:true
长度:null
固定:假
评论:''
firstname:
type:string
nullable:true
length:null
fixed:false
comment:''
公司:
type:string
nullable:true
length:null
固定:false
注释:''
functiontitle:
type:string
可空:true
长度:null
固定:false
注释:''
...
lifecycleCallbacks:{}
但是这没有
为了避免受伤,请在进一步阅读之前坐在你的手上。不要给自己一个面部的脑震荡。
...
准备好了吗? / p>
...
小写第一个字母的inheritanceType,discriminatorColumn和discriminatorMap。
删除架构并重建。
我假设您已经将discriminator列更改为discr。
I am currently setting up a new application and decided to use doctrine2 as an orm. I am new to doctrine2, and having quite some difficulties getting my design the way I want it.
This is what I'm trying to do:
I have the following classes:
class User {
protected $userid;
protected $role;
protected $lang;
protected $email;
protected $disabled;
...
+ getters and setters
}
class Person extends User {
protected $personid;
protected $name;
protected $firstname;
protected $company;
protected $functiontitle
...
+ getters and setters
}
class Candidate extends User {
protected $candidateid;
protected $name;
protected $firstname;
protected $title;
protected $birthdate;
...
+ getters and setters
}
Both Candidate and Person inherit the User properties, but have their own set op specific properties.
In the postgresql database I currently have a similar set up, 3 tables, with each their set of specific columns.
Now I would like to map this using yaml, and want all 3 of the classes to be accessible as entities. I also don't want to have to duplicate a lot of fields in my database. How can I accomplish this? I've been looking for solutions for days but haven't come up with anything working.
If what I am trying to do is not possible then please explain what my options are, and why this isn't supported. Without an ORM it seems that I could easily add foreign keys to candidate and person to join with the user table, but I can't get that to work with doctrine.
This is the yaml file I currently have to set it up as a Class Table Inheritance: User:
MyProject\model\User:
type: entity
table: `user`
id:
userid:
column: userid
type: integer
nullable: false
comment: ''
id: true
fields:
role:
column: role
type: string
nullable: true
length: null
fixed: false
comment: ''
email:
type: string
nullable: true
length: null
fixed: false
comment: ''
disabled:
type: boolean
nullable: true
comment: ''
InheritanceType: JOINED
DiscriminatorColumn:
name: discr
type: string
DiscriminatorMap:
person: Person
candidate: Candidate
lifecycleCallbacks: { }
Person:
MyProject\model\Person:
type: entity
table: person
fields:
name:
type: string
nullable: true
length: null
fixed: false
comment: ''
firstname:
type: string
nullable: true
length: null
fixed: false
comment: ''
company:
type: string
nullable: true
length: null
fixed: false
comment: ''
functiontitle:
type: string
nullable: true
length: null
fixed: false
comment: ''
...
lifecycleCallbacks: { }
But this doesn't work.
To avoid injury, sit on your hands before reading further. Don't want to give yourself a concussion with a face palm.
...
ready?
...
Lower case the first letter of inheritanceType, discriminatorColumn and discriminatorMap.
Drop the schema and rebuild.
I am assuming that you already changed the discriminator column to discr.
这篇关于Doctrine2设计遗产问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!