域对象可以调用其他数据映射器吗? (Zend 框架)

发布于 2024-10-03 05:40:03 字数 600 浏览 7 评论 0原文

例如:

我有一个用户,有 10 个小部件。除此之外,我还有一个 Manager 来管理其中 5 个小部件。

我想检索由指定管理员管理的用户小部件。因此,我在 WidgetMapper 中创建了一个名为 fetchUsersManagedWidgets($userId, $managerId) 的函数,该函数在数据库中查询这 5 个小部件并映射 Widget 对象的数组。

我知道域对象不应该知道它们的映射器,但是我可以在用户模型中创建一个调用 WidgetMapper 函数的函数吗?

例如

class Application_Model_User {

    public function getWidgetsManagedBy($manager) {
        $widgetMapper = new Application_Model_WidgetMapper;
        return $widgetMapper->fetchUsersManagedWidgets($this->getId(), $manager->getId());
    }

}

,或者这是一条单行道,域对象永远不应该调用映射器函数?

For example:

I have a User which has 10 Widgets. Along with that I have a Manager that manages 5 of those widgets.

I want to retrieve the User's Widgets managed by a specified Manager. So I created a function in my WidgetMapper called fetchUsersManagedWidgets($userId, $managerId) which queries the db for those 5 widgets and maps an array of Widget objects.

I know domain objects are not supposed to be aware of their mappers, but can I create a function in the User model which calls a function of the WidgetMapper?

e.g.

class Application_Model_User {

    public function getWidgetsManagedBy($manager) {
        $widgetMapper = new Application_Model_WidgetMapper;
        return $widgetMapper->fetchUsersManagedWidgets($this->getId(), $manager->getId());
    }

}

Or is this a one way street, and domain objects should never call mapper functions?

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

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

发布评论

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

评论(1

苍风燃霜 2024-10-10 05:40:03

一般来说,模式是如何解决常见问题的想法。他们既不是“必须这样做”也不是“其他一切都不好”。事实上,甚至有一个一个名称来表示不遵循故意模式的决策。
据我所知,人们倾向于认为“一个数据库表是一个域,因此每个数据库表都需要一个模型和一个映射器”。在你的情况下,我假设你有三个表:用户、小部件和保存这两个表之间的 n:m 关系的表(我将其称为 userwidgets)。
userwidgets 实际上是用户模型的一部分,没有自己的模型/映射器,并且不是 widget 模型的一部分。要解析用户模型中的这些小部件 ID,确实需要小部件映射器,当然这会导致您描述的问题。
可能有很多方法可以解决这个问题,我只是假设一个可重写的默认映射器:

Class UserModel
{
    $_widgetMapper = null;

    public function getWidgetMapper()
    {
        if(null === $this->_widgetMapper)
        {
             $this->setWidgetMapper(new DefaultWidgetMapper());
        }
        return $this->_widgetMapper();
    }

    public function setWidgetMapper($mapper)
    {
        // todo: make sure it's a mapper of the correct type
        $this->_widgetMapper = $mapper;
    }
}

如果您不想使用默认的小部件映射器,您只需在访问用户的小部件之前设置它(它们应该被加载)一经请求)。

Pattern in general are an idea of how you could solve a common problem. Their is neither a "have to do it that way" or "everything else is bad". In fact there's even a name for a decission not following patterns intentional.
From what I have seen people tend to think "one db-table is one domain so every db-table needs one model and one mapper". In your case i suppose you have three tables: users, widgets and the table holding the n:m relation between those two (i'll call it userwidgets).
The userwidgets is really part of the user-model and doesn't have a own model/mapper and is not part of the widget model. To resolve those widget ids in the usermodel do need the widget mapper of course which results in the problem you describe.
There are probably lot's of ways to resolve this, i simply assume a default-mapper which is overwritable:

Class UserModel
{
    $_widgetMapper = null;

    public function getWidgetMapper()
    {
        if(null === $this->_widgetMapper)
        {
             $this->setWidgetMapper(new DefaultWidgetMapper());
        }
        return $this->_widgetMapper();
    }

    public function setWidgetMapper($mapper)
    {
        // todo: make sure it's a mapper of the correct type
        $this->_widgetMapper = $mapper;
    }
}

In case you don't want to use the default widget-mapper you simply should set it before accessing the user's widgets (they should be loaded on demand).

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