查找(1234) 包括关系

发布于 2024-12-04 15:11:26 字数 586 浏览 1 评论 0原文

我有一个模型“Events”(Zend_Db_Table_Abstract),它与其他模型有各种关系。通常我想我会做这样的事情来找到它及其关系:

$events = new Events();
$event = $events->find($id)->current();
$eventsRelationship1 = $event->findDependentRowset('Relationship1');

由于关系已经建立,我想知道是否有任何类型的自动加入可用或其他东西。每次我获取事件时,我也需要拥有所有关系。目前我看到只有两种方法可以实现这一点:

  1. 自己构建查询,硬编码。不喜欢这样,因为它正在围绕已经建立的关系和“模型方法便利性”进行工作。
  2. 使用单个查询获取每个相关对象。这个也很难看,因为我必须触发太多查询。

当考虑获取一组多行时,这甚至更进一步。对于单个事件,我可能会多次查询数据库,但是当获取 100 行时,连接只是基本的。

那么,有谁知道通过使用这些关系创建联接的方法,或者除了对查询进行硬编码之外没有其他方法吗?

预先感

谢阿恩

I am having a model "Events" (Zend_Db_Table_Abstract) that's got various relationships to other models. Usually I think I would do something like this to find it and its relationships:

$events = new Events();
$event = $events->find($id)->current();
$eventsRelationship1 = $event->findDependentRowset('Relationship1');

As the relationship is already set up I am wondering if there's any sort of automatic join available or something. Every time I fetch my event I need to have all the relationships, too. Currently I see only two ways to achieve that:

  1. Build the query myself, hard coded. Don't like this, because it's working around the already set up relationship and "model method convenience".
  2. Fetch every related object with a single query. This one's ugly, too, as I have to trigger too many queries.

This goes even a step further when thinking about getting a set of multiple rows. For a single event I may query the database multiple times, but when fetching 100 rows joins are just elementary.

So, does anyone know a way to create joins by using those relationships or is there no other way than hardcoding the query?

Thanks in advance

Arne

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

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

发布评论

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

评论(1

昔日梦未散 2024-12-11 15:11:26

解决这一挑战的方法是“升级”数据库访问以使用 dataMapper 模式。

您本质上是在应用程序中的模型与其在数据库中的表示之间添加一个额外的层。该映射器层允许您从不同的表读取/写入数据,而不是一个模型和一个表之间的直接链接。

这是一个很好的教程。 (有一些部分你可以跳过 - 我省略了所有的 getter 和 setter,因为只有我在使用代码)。

当您刚刚使用 Zend_Db_Table_Abstract 时,需要花一些时间来了解它的工作方式,但这是值得的。

The way to solve this challenge is to 'upgrade' your database access to use the dataMapper pattern.

You are essentially adding an extra layer between the model in your application an their representation in the db. This mapper layer allows you read/write data from different tables - rather than a direct link between one model and one table.

Here is a good tutorial to follow. (There are some bits you can skip - I left out all the getters and setters as its just me using the code).

It takes a little while to get your head round the way it works, when you've just been using Zend_Db_Table_Abstract, but it is worth it.

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