2

how to join two tables from different databases using zf2? I would like to write the following query.

SELECT db1.table1.*, db2.table2.* FROM db1.table1
INNER JOIN db2.table2 ON db2.table2.field1 = db1.table1.field1

Please give some example?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
chinna
  • 69
  • 4
  • You can find the answer here too -> http://stackoverflow.com/questions/14354802/tablegateway-with-multiple-from-tables/14371056#14371056 – Diemuzi Jul 26 '13 at 17:16

1 Answers1

0

I didn't check the syntax but it's basically something like this:

$db = new Zend_Db_Adapter_Pdo_Mysql(array(
    'host'     => '127.0.0.1',
    'username' => 'webuser',
    'password' => 'xxxxxxxx',
    'dbname'   => 'test'
));
$select = $db->select()
             ->from( array( 'table1' => 'db1.table1' ), array( 'table1.*','table2.*' ) )
             ->joinInner( array( 'table2' => 'db2.table2' ), 'table2.field1 = table1.field1', array() );
$result = $select->query->fetchAll();
var_dump($result);

Edit: $db definition sourced from http://framework.zend.com/manual/1.12/en/zend.db.adapter.html

David Normington
  • 849
  • 8
  • 15
  • Thanks for the prompt response. Where did you define $db here? Could you please give full example? – chinna Jul 22 '13 at 10:21
  • `$db` is the adaptor. It could equally be the table instance. I've updated my answer. – David Normington Jul 22 '13 at 10:52
  • Thanks for the update. I appreciate your help! But, I am getting the following error. PHP Fatal error: Class 'XXX\\Model\\Zend_Db_Adapter_Pdo_Mysql' not found. I googled and tried to add the following. But, I am still getting the same error. Please help! require_once 'Zend/Db/Adapter/Pdo/Mysql.php'; or require_once 'Zend/Db/Adapter/Driver/Pdo/Pdo.php'; or require_once 'Zend/Db/Adapter/Driver/Pdo/Mysql.php'; or require_once 'Zend/Loader/Autoloader.php'; – chinna Jul 22 '13 at 11:44
  • I am assuming this code will work in zend framework 1.x not in zend framework 2.x. – chinna Jul 22 '13 at 11:57