PHP 学说:多态关联
我正在使用 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
、州 和
国家/地区
。或者地址
,如果我们想要非常具体的话。有关该类层次结构的更多信息:(
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、单表继承或类表继承)?
I am using Doctrine PHP ORM 2.0.
What I would like to implement, is the following class hierarchy. (Please note that the following code fragment will fail to execute, as it is neither syntactically correct PHP, nor correct usage of Doctrine annotations.)
@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
}
Now, for the subclasses of Location we could take City
, State
and Country
, for example. Or Adress
, if we want to be very specific. For a little more information on this class hierarchy:
class Adress { $parent; /* of type City, ... some other attributes */ }
class City { $parent; /* of type State */ }
class State { $parent; /* of type Country */ }
class Country {}
(The above hierarchy would make my answer lean towards a Table-per-Class solution.)
Questions:
- Is it possible to have a polymorphic attribute in an entity, and if so, how do I implement it?
- Which what inheritance strategy could I implement the Location class hierarchy (as seen below), and which strategies are preferred (i.e. MappedSuperclass, Single-Table or Class-Table inheritance)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,Doctrine 2 支持三种不同类型的继承:MappedSuperclass、Single Table 和 Class Table...如您所知。
Doctrine Reference 解释了如何实施它们。
这是一篇关于映射对象继承的好文章:将对象映射到关系数据库:O /R 映射详细信息
Yes, Doctrine 2 supports three different types of inheritance: MappedSuperclass, Single Table and Class Table... as you know.
The Doctrine Reference explains how to implement them.
Here is a good article you can read on mapping object inheritance: Mapping Objects to Relational Databases: O/R Mapping In Detail
在这种情况下,您需要单表继承或类表继承。因为您想要查询根(位置),出于性能原因,您应该更好地使用单表继承。
In this case you'd need Single- or Class-Table-Inheritance. Because you want to query for the root (Location) you should better use Single Table Inheritance for performance reasons.
是的,原则 2 会进行多态关联。使用
mappedBy
和inversedBy
的双向关联存在问题,但简单的情况可以正常工作。Yes, Doctrine 2 will do polymorphic associations. There issues with bi-directional associations using
mappedBy
andinversedBy
, but the simple case works fine.