多表查询的面向对象表示

发布于 2024-09-15 17:54:54 字数 707 浏览 6 评论 0原文

假设我们有两个相关的表,例如一个代表一个人:

PERSON

  • name
  • Age
  • ...
  • current_status_id

,另一个代表此人在特定时间的状态更新:

STATUS_HISTORY

  • returned_on
  • status_id
  • Blood_Pressure
  • length
  • ...

我已经用 PHP 构建了一个应用程序使用 Zend Framework,并尝试通过使用一个类来表示一个人以及一个类来表示一个人的状态来保留“面向对象”。我还尝试尽可能使用 ORM 原则,例如使用数据映射器将领域模型与数据层分离。

从数据映射器返回人员列表的一种很好的(面向对象的)方式是什么,在列表中我有时想知道该人最后测量的血压,有时不想知道(取决于报告的要求) /view 其中使用列表)。这同样适用于不同的字段,例如在数据层计算的值(总和、计数等)。

我的第一个想法是使用行集(例如 Zend_Db_Rowset),但这在我的视图和数据层之间引入了高耦合。另一种方式可能是返回人员列表,然后使用数据映射器查询每个人的最新状态以请求特定人员的状态。但是,这将导致(至少)对每条记录进行一次额外的查询,并且不允许我在数据层使用 JOINS。

有什么建议吗?

Suppose we have two related tables, for example one representing a person:

PERSON

  • name
  • age
  • ...
  • current_status_id

and one representing a status update at a specific time for this person:

STATUS_HISTORY

  • recorded_on
  • status_id
  • blood_pressure
  • length
  • ...

I have built an application in PHP using Zend Framework, and tried to retain 'object orientedness' by using a class for representing a person and a class for representing the status of a person. I also tried to use ORM principles where possible, such as using the data mapper for separating the domain model from the data layer.

What would be a nice (and object oriented) way of returning a list of persons from a data mapper, where in the list I sometimes want to know the last measured blood_pressure of the person, and sometimes not (depending on the requirements of the report/view in which the list is used). The same holds for different fields, e.g. values computed at the data layer (sum's, count's, etc.).

My first thought was using a rowset (e.g. Zend_Db_Rowset) but this introduces high coupling between my view and data layer. Another way might be to return a list of persons, and then querying for each person the latest status using a data mapper for requesting the status of a specific person. However, this will result in (at least) one additional query for each person record, and does not allow me to use JOINS at the data layer.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

半衾梦 2024-09-22 17:54:54

由于我工作的 ORM,我们也遇到了同样的问题。如果您非常担心必须首先获取人员列表,然后单独查询他们的状态对性能的影响,那么您实际上别无选择,只能稍微耦合一下数据。

在我看来,这没关系。您可以创建一个类来保存单个“人”数据和一个包含“status_history”记录的数组,或者遭受对每个“人”进行另一个查询的性能影响。您可以通过在本地进行数据缓存来减少查询开销(您的控制器必须决定,如果在特定时间阈值之前发出对一组数据的请求,它只返回自己的数据,而不是查询数据库服务器

)纯粹的面向对象视图很好,但有时不切实际。

We have this same issue because of our ORM where I work. If you are worried enough about the performance hit of having to first get a list of your persons, then query for their statuses individually, you really have no other choice but to couple your data a little bit.

In my opinion, this is okay. You can either create a class that will hold the single "person" data and an array containing "status_history" records or suffer the performance hit of making another query per "person". You COULD reduce your query overhead by doing data caching locally (your controller would have to decide that if a request for a set of data is made before a certain time threshold, it just returns its own data instead of querying the db server)

Having a pure OO view is nice, but sometimes impractical.

魄砕の薆 2024-09-22 17:54:54

尝试使用PHP的内置类“stdclass”,您可以获得PHP自动创建的stdclass对象,其成员变量将是列名。所以你可以获取对象并通过列名获取值。例如。

查询是

SELECT a.dept_id,a.dept_name,a.e_id,b.emp_name,b.emp_id from DEPT a,EMP b where b.emp_id=a.e_id;

结果将是 stdclass 对象的数组。每一行代表一个 stdclass 对象。

对象

标准类
{
部门 ID;
部门名称;
e_id;
员工 ID;
员工姓名;

您可以访问
foreach($结果集为$行)
{
$d_id = $row->dept_id;
$d_nam= $row->dept_name;
$e_id = $row->e_id;
$em_id= $row->emp_id;
$e_nam= $row->emp_name;
}

块引用

我不确定性能。

Try to use "stdclass" class which is PHP's inbuild class, You can get the object of stdclass which will be created automatically by PHP and its member variable will be column name. So u can get object and get the values by column name. For example.

Query is

SELECT a.dept_id,a.dept_name,a.e_id,b.emp_name,b.emp_id from DEPT a,EMP b where b.emp_id=a.e_id;

Result will be array of stdclass objects. Each row represents one stdclass object.

Object

STDCLASS
{
dept_id;
dept_name;
e_id;
emp_id;
emp_name;
}

You can access like
foreach($resultset as $row)
{
$d_id = $row->dept_id;
$d_nam= $row->dept_name;
$e_id = $row->e_id;
$em_id= $row->emp_id;
$e_nam= $row->emp_name;
}

But

Blockquote

I am not sure about performance.

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