Doctrine2设计继承问题

时间:2014-10-09 13:15:15

标签: doctrine-orm mapping

我目前正在设置一个新的应用程序,并决定使用doctrine2作为一个orm。 我是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
}

Candidate和Person都继承了User属性,但是有自己的set op特性属性。

在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 个答案:

答案 0 :(得分:1)

为避免受伤,请在进一步阅读前先坐在您的手上。不要用脸掌给自己一个脑震荡。

...

就绪

...

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

删除架构并重建。

我假设你已经将discriminator列更改为discrimin。