jmarais
jmarais

Reputation: 93

Accessing data from another database in Joomla 3.2+

I am upgrading my components from Joomla 1.7 to Joomla 3.3 and have to keep the original database. Therefore I need to access the display data from another database as the installation database. I tried a method that I used many times before with Joomla 2.5 but it seems that I cannot get it right this time.

In my model in the getListQuery() method (which overrides the modellist method) I use the following code to access the database from where I want to get my data:

$dbOptions = getDbOptions();
$db = & JDatabase::getInstance($dbOptions); 

where the connection details of my old database are contain in $dbOptions.

I continue to use the following code:

$query = $db -> getQuery(true);
$query -> select('*') -> from('table');
return $query;

I do include the following in the beginning:

jimport('joomla.application.component.modellist');

modellist extends JModelLegacy, therefore I do believe that it uses the following:

/libraries/legacy/model/list.php

But it gives me an error that the table newDatabase.table does not exits and therefore the method I am using does not connect to my old database to retrieve the data from oldDatabase.table.

I am unsure about the inclusion of jimport('joomla.application.component.modellist'); though, could be the problem?

Anyone who can help to retrieve the data from my original database?

Upvotes: 1

Views: 2285

Answers (4)

Roddy P. Carbonell
Roddy P. Carbonell

Reputation: 865

Using Joomla 2.5, this approach worked for me:

 $option = array();
   $option['driver']   = 'mysql';
   $option['host']     = 'localhost';
   $option['user']     = 'XXXXXXXX';
   $option['password'] = 'XXXXXXXX';
   $option['database'] = 'wordpress';
   $option['prefix']   = 'jtt_';
   $db = JDatabase::getInstance( $option );
   $query = "select * from #__categories";
   $db->setQuery($query);

I was able to connect to another database just fine.

Upvotes: 0

e-motiv
e-motiv

Reputation: 5893

I believe Adam B's code can have some improvement, regarding setting DB.

public function __construct($config = array())
{
    $options = array(
        'driver'   => 'mysqli',
        'host'     => 'localhost',
        'user'     => 'username',
        'password' => 'password',
        'database' => 'database',
        'prefix'   => 'jos_'
    );

    //parent construct will handle setting the DB
    $config['dbo']=JDatabase::getInstance( $options );

    parent::__construct($config);
}

And getting DB: I think JFactory::getDbo(); will give you the wrong DB, you should do:

self:getDbo();

Upvotes: 1

Alex Karolik
Alex Karolik

Reputation: 31

If old database on same server and active mysql user has access for it you can use such sql query:

$query = $db -> getQuery(true);
$query -> select('*') -> from('old_database.table');
return $query;

Upvotes: 2

Adam B
Adam B

Reputation: 1158

If the entire model is just fetching data from the external database you could use JDatabase->setDbo to replace the default database object with your custom one.

public function __construct($config = array())
{
    parent::__construct($config);

    $options = array();
    $options['driver']   = 'mysqli';
    $options['host']     = 'localhost';
    $options['user']     = 'username';
    $options['password'] = 'password';
    $options['database'] = 'database';
    $options['prefix']   = 'jos_';

    $db = JDatabase::getInstance( $options );
    parent::setDbo($db);
}

Now you should be able to access the database in getListQuery() just as you would with your default database. E.g.

$db = JFactory::getDbo();

Upvotes: 1

Related Questions