zf2 make a join between two different databases

后端 未结 5 1931
感情败类
感情败类 2021-01-13 16:31

I am trying to make a join between two tables placed in different databases with Zend Framework 2.

The first table is called users and is stored in

5条回答
  •  甜味超标
    2021-01-13 16:41

    Looks like this question was asked a while back, but I seem to have found a good workaround or solution. If you utilize the Zend\Db\Sql\TableIdentifier and Zend\Db\Sq\Expression, you will be able to get around your issue.

    public function getSelect(Hierarchy $hierarchy) {
        $select = $this->tableGateway->getSql()->select();
        $select->where(array('level' => $hierarchy()->getId()));
        $select->join(
             array('h' => new TableIdentifier('hierarchies', 'admin')), 
             new Expression('h.id = ?', 'users.idHierarchy', Expression::TYPE_IDENTIFIER), 
             array('hierarchyId' => 'id', 'level' => 'level')
        );
        return $select;
    }
    

    I wasn't sure which database your hierarchies table is in so I used 'admin' for now. YOu can replace it with which ever database name you have. See if it works for you, seems to work nicely for me.

提交回复
热议问题