0

In an app we are developing, we have Services, Mappers and Entities. We are not using an ORM. In the app, we have Group, GroupMember & Member entities. The GroupMember entity has the groupId, memberId & memberAccess properties. The memberAccess fields tells us the access level of the Member within the Group. Now we need to fetch

  1. all the Groups where a member has specific level of access. (by providing a memberId)
  2. all the Groups where a member is a member along with his access. (by providing a memberId)
  3. all the Members within a group with their access. (by providing a groupId)

any ideas which service(s) should be used for each of the following. and how they will interact to fetch the specific data.

Bryan
  • 645
  • 1
  • 6
  • 18

1 Answers1

3

1) MembershipMapper: implement your queries here returning arrays of entities (have a look at ZfcBase\Mapper\AbstractDbMapper as base class)

function getGroupsForMember($member, $access = null)
function getMembersForGroup($group, $access = null)

2) MembershipServive: in Module.php under getServiceConfig you add this service to the service manager. Use a closure to create a new MembershipService and inject a MembershipMapper into it. The groups/members functions will probably just be proxies to the mapper.

function setMembershipMapper($membershipMapper)
function getMembershipMapper()
function getGroupsForMember($member, $access = null)
function getMembersForGroup($group, $access = null)

3) Controller: inject your MembershipService into your controller (you can do this in the same way you create your services with a closure). Then call the methods on the service.

Example for creating a service/mapper/controller in the service/controller manager (goes in Module.php)

public function getServiceConfig() 
{
    return array(
        'factories' => array(
            'MembershipService' => function (Zend\ServiceManager\ServiceManager $sm) {
                $service = new YourNS\Service\Membership();
                $service->setMembershipMapper($sm->get('MembershipMapper'));
                return $service;
            },
            'MembershipMapper' => function ($sm) {
                $mapper = new \YourNS\Mapper\Membership();
                return $mapper;
            },
    );
}

public function getControllerConfig() 
{
    return array(
        'factories' => array(
            'YourNS\Controller\Something' => function (Zend\Mvc\Controller\ControllerManager $cm) {
                $controller = new YourNS\Controller\Something();
                $controller->setMembershipService($cm->getServiceLocator()->get('MembershipService'));
                return $controller;
            },
}
Jeroen
  • 982
  • 1
  • 6
  • 14
  • thanks. still confused. If I need to get all the groups a member belongs to, access + the group creator, I'll use the `getGroupsForMember`, it will return an array of `Membership` objects(`groupId`,`userId`,`access`). Now how to display the name of `Group` and the group creator. The `Group` has the `creatorId` property. So I think I will need to loop through each membership, get the `groupId`, create a `Group` object, get the `creatorId` from `Group` and again visit the persistent to create the `User` object of the creator. So I will endup with 2*NoOfGroups DB trips. – Bryan Oct 22 '13 at 13:22