2014-10-09 137 views
0

我目前正在设置一个新的应用程序,并决定使用doctrine2作为orm。 我是新来的doctrine2,并有一些困难让我的设计符合我的要求。Doctrine2设计继承问题

这就是我想要做的事:

我有以下类别:

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 
} 

两个候选人和人继承用户属性,但有自己的一套运算的特定属性。

在postgresql数据库中,我现在有一个类似的设置,3个表格,每个表格都有一组特定的列。

现在我想使用yaml来映射它,并希望所有3个类都可以作为实体访问。 我也不想在我的数据库中复制很多字段。 我该如何做到这一点? 我一直在寻找解决方案的天,但还没有拿出任何工作。

如果我正在尝试做的事情是不可能的,那么请解释我的选择是什么,以及为什么这不被支持。 没有ORM,我似乎可以很容易地将外键添加到候选人和人员加入用户表,但我不能让它与教条一起工作。

这是YAML文件我目前必须将其设置为一个类表继承: 用户:

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: { } 

人:

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: { } 

但是,这是行不通的。

+1

看看D2的继承:http://doctrine-orm.readthedocs.org/en/latest/reference/inheritance-mapping.html#class-table-inheritance – Cerad 2014-10-09 13:41:58

+0

我有,而且目前有它设置像这个,但我现在得到一个错误,我的无用性的字段(即“角色”)不存在作为我的人表中的一列。这是正确的,因为我不希望他们在我的人员表中,因为他们在我的用户表中。 – 2014-10-09 13:50:05

+0

使用分解的映射文件更新您的问题。那很快。对于三个表,您希望使用类表继承,而不是单表继承。单桌会把一切放在一张桌子上。 – Cerad 2014-10-09 14:02:07

回答

1

为避免受伤,请在继续阅读之前坐在您的手上。不想给自己一个面部手掌脑震荡。

...

准备好了吗?

...

小写inheritanceType,discriminatorColumn和discriminatorMap的第一个字母。

删除架构并重建。

我假设您已经将鉴别器列更改为discr。

+0

非常感谢!然而我确实facepalm ;-) – 2014-10-09 14:57:20