简体   繁体   English

在Zend Framework中采用数据映射器方法

[英]Taking the data mapper approach in Zend Framework

Let's assume the following tables setup for a Zend Framework app. 假设为Zend Framework应用程序设置了以下表格。

user (id)
groups (id)
groups_users (id, user_id, group_id, join_date)

I took the Data Mapper approach to models which basically gives me: 我将Data Mapper方法用于模型,这基本上可以为我提供:

  • Model_User , Model_UsersMapper , Model_DbTable_Users Model_UserModel_UsersMapperModel_DbTable_Users
  • Model_Group , Model_GroupsMapper , Model_DbTable_Groups Model_GroupModel_GroupsMapperModel_DbTable_Groups
  • Model_GroupUser , Model_GroupsUsersMapper , Model_DbTable_GroupsUsers (for holding the relationships which can be seen as aentities; notice the "join_date" property) Model_GroupUserModel_GroupsUsersMapperModel_DbTable_GroupsUsers (用于保存可以视为实体的关系;请注意“ join_date”属性)

I'm defining the _referenceMap in Model_DbTable_GroupsUsers: 我在Model_DbTable_GroupsUsers中定义_referenceMap:

protected $_referenceMap = array (
    'User' => array (
        'columns'       => array('user_id'),
        'refTableClass' => 'Model_DbTable_Users',
        'refColumns'    => array('id')
    ),
    'App' => array (
        'columns'       => array('group_id'),
        'refTableClass' => 'Model_DbTable_Groups',
        'refColumns'    => array('id')
    )
);

I'm having these design problems in mind: 我正在考虑以下设计问题:

1) The Model_Group only mirrors the fields in the groups table. 1) Model_Group仅镜像组表中的字段。 How can I return a collection of groups a user is a member of and also the date the user joined that group for every group? 如何返回用户所属组的集合以及每个组的用户加入该组的日期? If I just added the property to the domain object, then I'd have to let the group mapper know about it, wouldn't I? 如果我只是将属性添加到域对象,则必须让组映射器知道它,不是吗?

2) Let's say I need to fetch the groups a user belongs to. 2)假设我需要获取用户所属的组。 Where should I put this logic? 我应该把这个逻辑放在哪里? Model_UsersMapper or Model_GroupsUsersMapper ? 是Model_UsersMapper还是Model_GroupsUsersMapper

I also want to make use of the referencing map (dependent tables) mechanism and probably use findManyToManyRowset or findDependentRowset, something like: 我还想利用引用映射(依赖表)机制,并可能使用findManyToManyRowset或findDependentRowset,例如:

$result = $this->getDbTable()->find($userId);
$row = $result->current();

$groups = $row->findManyToManyRowset(
    'Model_DbTable_Groups',
    'Model_DbTable_GroupsUsers'
);

This would produce two queries when I could have just written it in a single query. 当我将其编写为单个查询时,这将产生两个查询。 I will place this in the Model_GroupsUsersMapper class. 我将其放置在Model_GroupsUsersMapper类中。

An enhancement would be to add a getGroups method to the Model_User domain object which lazily loads the groups when needed by calling the appropriate method in the data mapper, which begs for the second question. 一种增强功能是将一个getGroups方法添加到Model_User域对象,该对象在需要时通过在数据映射器中调用适当的方法来延迟加载组,这引出了第二个问题。 Should I allow the domain object know about the data mapper? 我应该让域对象知道数据映射器吗?

This can be pretty confusing issue because a relational database can be hard to map to an object model. 这可能是一个非常令人困惑的问题,因为关系数据库可能很难映射到对象模型。

I'd be inclined to focus on your object model requirements first. 我倾向于首先关注您的对象模型需求。 For example if in your object model it makes sense for your user object to (almost) always be aware of the groups then this relationship should be incorporated into the user class. 例如,如果在您的对象模型中让您的用户对象(几乎)始终知道组是有意义的,则应将此关系合并到用户类中。 If on the other hand you often need to use users without needing to know what groups they are a part of perhaps you can have two user classes, a base class and a group aware extended version (base_user and group_user). 另一方面,如果您经常需要使用用户而不需要知道他们属于哪些组,则可以拥有两个用户类,即基类和可识别组的扩展版本(base_user和group_user)。

I would try to avoid letting the domain object know about the data layer as that's kind of the whole point to using this pattern. 我会尽量避免让域对象知道数据层,因为这是使用此模式的全部要点。 The data layer should pretty much just be a dumb factory that instantiates your domain objects. 数据层几乎应该只是一个实例化域对象的哑工厂。

Just my take on it :) 只是我的看法:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM