Assume the following tables are installed for the Zend Framework application.
user (id) groups (id) groups_users (id, user_id, group_id, join_date)
I applied the Data Mapper approach to models that basically give me:
- Model_User , Model_UsersMapper , Model_DbTable_Users
- Model_Group , Model_GroupsMapper , Model_DbTable_Groups
- Model_GroupUser , Model_GroupsUsersMapper , Model_DbTable_GroupsUsers (to save relations that can be seen as creatures, pay attention to the join_date property)
I define _referenceMap in Model_DbTable_GroupsUsers:
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 design issues:
1) Model_Group displays only the fields in the group table. How can I return a collection of groups of which the user is a member, as well as the date the user joined this group for each group? If I just added the property to the domain object, then I would have to report this to the group mapper, right?
2) Let's say I need to get the groups the user belongs to. Where should I put this logic? Model_UsersMapper or Model_GroupsUsersMapper ?
I also want to use the map binding mechanism (dependent tables) and probably use findManyToManyRowset or findDependentRowset, something like:
$result = $this->getDbTable()->find($userId); $row = $result->current(); $groups = $row->findManyToManyRowset( 'Model_DbTable_Groups', 'Model_DbTable_GroupsUsers' );
This will create two queries when I could just write them in one query. I put this in the Model_GroupsUsersMapper class.
An improvement will be to add the getGroups method to the Model_User domain object, which lazily loads groups when necessary, by calling the appropriate method in the data converter, which requires a second question. Should I let a domain subject know about a data converter?