简体   繁体   中英

Joomla 2.5 - Multiple databases query through API

I'm developing a custom multishop component for Joomla 2.5. I know, it's a lot of work, but i'm on it, and all is working quite well for the moment ;)

I'm dealing with 2 databases :

  • a master, which contains all main informations on products, with a classical structure (products, categories, product-categories relations, manufacturers…)
  • a slave, which contains only local informations (product_id & some custom stuff like selling prices for this particular shop).

The process to create queries on the master OR the local db is ok. An easy getDbo() for local db, and a getInstance for master db with new params in the array like

$masterdb = & JDatabase::getInstance( $master_options );

What if i want to make a "crossed query" between both dbs with JOINS ? 如果我想在两个数据库之间使用JOINS进行“交叉查询”怎么办? like :

SELECT * 
FROM slave_db.mytable 
LEFT JOIN master_db.othertable
ON slave_db.mytable.column_name=master_db.othertable.column_name;

Does the framework of Joomla allow that ?

If it doesn't, i think i could either write hard-coded request, out of joomla's framework way of doing it… but i don't really like the idea… or i could maybe do separate request and work only in PHP to compare/merge/reorganize arrays… but it sucks too !

Please help ! ;)

OK, here is how i achieved to do that ! It's a bit sneaky, but it works…

// For Master DB calls.
$master_options['driver']   = MASTER_DRIVER;
$master_options['host']     = MASTER_HOST;
$master_options['user']     = MASTER_USER;
$master_options['password'] = MASTER_PASSWORD;
$master_options['database'] = MASTER_DB;
$master_options['prefix']   = MASTER_PREFIX;
$masterdb = JDatabase::getInstance( $master_options );

$query = $masterdb->getQuery(true);

//slave infos
$app            = JFactory::getApplication();
$slave_prefix   = $app->getCfg('dbprefix');
$slave_name     = $app->getCfg('db');

$query = "SELECT * 
FROM `".$slave_name."`.`".$slave_prefix."mytable`
LEFT JOIN `".$master_options['database']."`.`".$master_options['prefix']."othertable`
ON `".$slave_name."`.`".$slave_prefix."mytable`.`column_name`=`".$master_options['database']."`.`".$master_options['prefix']."othertable`.`column_name`";

$masterdb->setQuery((string)$query);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM