【发布时间】:2011-01-17 21:48:04
【问题描述】:
我正在使用Doctrine PHP ORM 2.0。
我想要实现的是以下类层次结构。 (请注意,以下代码片段将无法执行,因为它既不是语法正确的 PHP,也不是正确使用 Doctrine 注释。)
@MappedSuperclass
abstract class Location {}
@Entity
class GeoId {
@Column(type = "float") $latitude;
@Column(type = "float") $longitude;
// this is the part that my question concerns
@???
$location; // any subclass of location can go here
}
现在,对于 Location 的子类,我们可以采用 City、State 和 Country 为例。或者Adress,如果我们想要非常具体的话。有关此类层次结构的更多信息:
class Adress { $parent; /* of type City, ... some other attributes */ }
class City { $parent; /* of type State */ }
class State { $parent; /* of type Country */ }
class Country {}
(上面的层次结构会使我的答案倾向于每类表的解决方案。)
问题:
- 实体中是否可以具有多态属性,如果可以,如何实现?
- 我可以采用哪种继承策略来实现 Location 类层次结构(如下所示),以及首选哪种策略(即 MappedSuperclass、Single-Table 或 Class-Table 继承)?
【问题讨论】:
标签: php inheritance doctrine-orm polymorphic-associations