【问题标题】:Symfony2/Doctrine: Reuse Unique base Entity for inheritance entitiesSymfony2/Doctrine:重用继承实体的唯一基础实体
【发布时间】:2014-06-18 11:42:25
【问题描述】:

在我的软件中,我有三个实体:Person、Teacher 和 Examiner,结构如下:

<?php

class Person
{

    protected $id;

    protected $firstname;

    protected $lastname;
}

class Teacher extends Person
{
    protected $subjects;
}

class Examiner extends Person
{
    protected $certificates;
}

这是我的问题:我计划使用 JOINED Class Table 进行继承,我希望 Person 成为一个独特的实体。但我也想为 Examiner 和 Teacher 重用它。所以我有 两个不同的实体 Examiner 和 Teacher,但 他们都有一个基本实体人

例如:

Person | id | firstname | lastname
       | 1  | Emile     | Example

Teacher | id | subjects
        |  1 | Math, Religion, English   <--- Inheritance of Person with id 1

Examiner | id | certificate
         | 1  | Bachelor of Science   <--- Inheritance of Person with id 1, too

有没有可能通过 Doctrine 注释来实现这一点?我不喜欢将实体 person 作为两个显式类的关系的想法。我更喜欢重现真实世界的情景,例如老师没有人,他是人。

希望任何人对我想要实现的目标有所了解。

谢谢!

【问题讨论】:

    标签: php symfony doctrine-orm entity


    【解决方案1】:

    我猜你应该声明一个抽象的 MappedSuperClass “human” 例如,它包含公共属性和方法。它不会被持久化在数据库中。

    然后声明继承human的person、teacher和examiner,并声明各自的具体属性和方法。这就是将存储在数据库中的具体表。

    这应该会有所帮助:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html

    问候。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多