如何使用ZFcBase在ZF2中指定表之间的关系

i'm new in Zend Framework. I'm working with an application that uses the following db pattern: Controler -> Service -> Mapper. And i have an entity for each table and a hydrator file. What i need is to join tables in SQL query, but i don´t know how to access the results from the joined table, cause it only feeds the entity of the current table. Here´s my code:

Controller:

$userEntity = new \Portal\Entity\Iamuserdb();
$userEntity->setIamUserDbId($this->params('id'));
$service = $this->getServiceLocator()->get('Portal\Service\Reports');
$user = $service->getUserById($userEntity)

Service (Reports):

protected $iamuserMapper;

public function selectUsers(){
$usuarios = $this->getIamUserMapper()->fetchAllUsers();
return $usuarios;
}

protected function getIamUserMapper()
{
if ($this->iamuserMapper == null) {
$this->iamuserMapper = $this->getServiceManager()->get('Portal\Mapper\Iamuser');
}
return $this->iamuserMapper;
}

Mapper (Iamuser):

public function fetchAllUsers()
{
$select = $this->getSelect();
$select->from('IAMUser')
->join('IAMUser_IAMGroup', 'IAMUser.iamUserDbId=IAMUser_IAMGroup.IAMUser_iamUserDbId', array('IAMUser_iamUserDbId', 'iamGroups_iamGroupDbId'), 'left')
->join('IAMGroup', 'IAMGroup.iamGroupDbId=IAMUser_IAMGroup.iamGroups_iamGroupDbId', array(), 'left');
return $this->select($select);
}

Hydrator:

class Iamuser extends Hydrator
{
protected function getEntity()
{
return 'Portal\Entity\Iamuserdb';
}

protected function getMap()
{
return array(
);
}
}

In my Module.php from the module i have the factory:

'factories' => array(
'Portal\Mapper\Iamuser' => function($sm) {
$dbAdapter = new \Zend\Db\Adapter\Adapter($sm->get('Configuration')['db']);
$mapper = new Mapper\Iamuser();
$mapper->setEntityPrototype(new Entity\Iamuserdb())
->setHydrator(new Mapper\Hydrator\Iamuser())
->setDbSlaveAdapter($dbAdapter)
->setDbAdapter($dbAdapter);
return $mapper;
},
}

See in my mapper i'm joining two tables, but how to fetch these results, since they came from another table and is not feed in Entity class? Thanks in advance

查看全部
dongmi1872
dongmi1872
2014/08/19 21:14
  • php
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复