PHP 学说:多态关联

发布于 2024-10-13 02:59:37 字数 1017 浏览 9 评论 0原文

我正在使用 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 {}

上面的层次结构将使我的答案倾向于每类表解决方案。)

问题:

  1. 实体中是否可能具有多态属性,如果可以,我该如何做实施吗?
  2. 我可以采用哪种继承策略来实现 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:

  1. Is it possible to have a polymorphic attribute in an entity, and if so, how do I implement it?
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜未央樱花落 2024-10-20 02:59:37

是否可以实现多态
实体中的属性,如果是,如何
我要实施它吗?

是的,Doctrine 2 支持三种不同类型的继承:MappedSuperclass、Single Table 和 Class Table...如您所知。
Doctrine Reference 解释了如何实施它们。

这是一篇关于映射对象继承的好文章:将对象映射到关系数据库:O /R 映射详细信息

Is it possible to have a polymorphic
attribute in an entity, and if so, how
do I implement it?

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

与酒说心事 2024-10-20 02:59:37

在这种情况下,您需要单表继承或类表继承。因为您想要查询根(位置),出于性能原因,您应该更好地使用单表继承。

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.

沉睡月亮 2024-10-20 02:59:37

是的,原则 2 会进行多态关联。使用 mappedByinversedBy 的双向关联存在问题,但简单的情况可以正常工作。

/** @Entity */
class GeoId {
  /** @Column(type = "float") */ 
  private $latitude;
  /** @Column(type = "float") */ 
  private $longitude;

  /** @OneToOne(targetEntity="Location") */ 
  private $location;
}

Yes, Doctrine 2 will do polymorphic associations. There issues with bi-directional associations using mappedBy and inversedBy, but the simple case works fine.

/** @Entity */
class GeoId {
  /** @Column(type = "float") */ 
  private $latitude;
  /** @Column(type = "float") */ 
  private $longitude;

  /** @OneToOne(targetEntity="Location") */ 
  private $location;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文