I am trying to make a connection between two tables hosted on different databases with Zend Framework 2.
The first table is called users and is stored in the admin database
The second table is called a hierarchy and is stored in the client database
I load database 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 to UserDao using 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 generates this SQL statement:
SELECT "users". *, "h". "id" AS "hierarchyId", "h". "level" AS "level" FROM "users Initials INNER JOIN" 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 am trying to specify an int join database name as follows:
$ select-> join (array ('h' => 'customer.hierarchies'), 'h.id = users.idHierarchy', array ('hierarchyId' => 'id', 'level' => 'level') );
But this also raises this exception:
SQLSTATE [42S02]: No base table or view found: 1146 Table "admin.customer.hierarchies" does not exist
I found this website where I explained how I can do this, but it is only valid for Zend Framework 1, and I am working with Zend Framework 2.
Using different databases with the Zend Framework
Can someone help me? You are welcome.
Thanks!