dongmi1872 2014-08-19 21:14
浏览 42
已采纳

如何使用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

  • 写回答

1条回答 默认 最新

  • doupang5433 2014-08-21 21:16
    关注

    I Solve my problem. All i have to do is return this statement:

    return $this->select($select)->getDataSource();
    

    So i skip the hydration results in entities and get raw PDO array, this way i can join tables.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题