1)MembershipMapper:實現您的疑問在這裏返回實體陣列(看看ZfcBase \映射\ AbstractDbMapper作爲基類)
function getGroupsForMember($member, $access = null)
function getMembersForGroup($group, $access = null)
2)MembershipServive:在Module.php下getServiceConfig將此服務添加到服務管理器。使用閉包創建一個新的MembershipService並向其中注入一個MembershipMapper。組/成員函數可能只是代理映射器。
function setMembershipMapper($membershipMapper)
function getMembershipMapper()
function getGroupsForMember($member, $access = null)
function getMembersForGroup($group, $access = null)
3)控制器:注入你MembershipService到控制器中(你可以在你與閉合創建服務以同樣的方式做到這一點)。然後調用服務上的方法。
實施例在服務/控制器管理器創建服務器/映射器/控制器(進去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;
},
}
感謝。仍然困惑。如果我需要獲得一個成員所屬的所有組,那麼訪問+組創建者,我將使用'getGroupsForMember',它將返回一個包含'Membership'對象的數組('groupId','userId','access' )。現在如何顯示「Group」和組創建者的名字。 'Group'具有'creatorId'屬性。所以我想我需要遍歷每個成員,獲取'groupId',創建一個'Group'對象,從'Group'獲取'creatorId'並再次訪問持久化以創建創建者的User'對象。所以我最終會以2 * NoOfGroups DB旅程結束。 – Bryan