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 the database admin
The second table is called hierarchy and is stored in the database customer
I load the databases adapters in global.php
return array( 'admin' => array( 'driver' => 'Pdo', 'dsn' => 'mysql:dbname=admin;host=localhost', 'driver_options' => array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'' ), ), 'customer' => array( 'driver' => 'Pdo', 'dsn' => 'mysql:dbname=customer;host=localhost', 'driver_options' => array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'' ), ), 'service_manager' => array( 'factories' => array( 'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory', ), ),
);
But when I try to make a join in UserDao with this function:
public function getSelect(Hierarchy $hierarchy) { $select = $this->tableGateway->getSql()->select(); $select->where(array('level' => $hierarchy()->getId())); $select->join(array('h' => 'hierarchies'), 'h.id = users.idHierarchy', array('hierarchyId' => 'id', 'level' => 'level')); return $select; }
This generate this SQL sentence:
SELECT "users".*, "h"."id" AS "hierarchyId", "h"."level" AS "level" FROM "users" INNER JOIN "hierarchies" AS "h" ON "h"."id" = "users"."idHierarchy" WHERE "level" = '1'
But it throws this exception when I try to use it:
Zend\Db\Adapter\Exception\InvalidQueryException SQLSTATE[42S02]: Base table or view not found: 1146 Table 'admin.hierarchies' doesn't exist
I try to indicate the name of the database int the join like this:
$select->join(array('h' => 'customer.hierarchies'), 'h.id = users.idHierarchy', array('hierarchyId' => 'id', 'level' => 'level'));
But it throws this exception too:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'admin.customer.hierarchies' doesn't exist
I found this web where explained how I can do it, but it's only valid for Zend Framework 1 and I'm working with Zend Framework 2.
Using Different Databases with Zend Framework
Could somebody help me? Please.
Thanks!