zend framework2 - zf2 make a join between two different databases -
i trying make join between 2 tables placed in different databases zend framework 2.
the first table called users , stored in database admin
the second table called hierarchy , stored in database customer
i load 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 try make join in userdao 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 sql sentence:
select "users".*, "h"."id" "hierarchyid", "h"."level" "level" "users" inner join "hierarchies" "h" on "h"."id" = "users"."idhierarchy" "level" = '1'
but throws exception when try use it:
zend\db\adapter\exception\invalidqueryexception sqlstate[42s02]: base table or view not found: 1146 table 'admin.hierarchies' doesn't exist
i try indicate name of database int join this:
$select->join(array('h' => 'customer.hierarchies'), 'h.id = users.idhierarchy', array('hierarchyid' => 'id', 'level' => 'level'));
but throws exception too:
sqlstate[42s02]: base table or view not found: 1146 table 'admin.customer.hierarchies' doesn't exist
i found web explained how can it, it's valid zend framework 1 , i'm working zend framework 2.
using different databases zend framework
could me? please.
thanks!
looks question asked while back, seem have found workaround or solution. if utilize zend\db\sql\tableidentifier , zend\db\sq\expression, able around 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 database hierarchies table in used 'admin' now. can replace ever database name have. see if works you, seems work nicely me.
Comments
Post a Comment