I’ve been recently asked how you can connect to a database server different than the “master”, that is the server where the PHPDS installation resides.

Actually it’s very easy as long as the server is Mysql:

class test_connector extends PHPDS_legacyConnector
{
 protected function applyConfig($db_config = '')
 {
  $this->dbHost = "localhost";
  $this->dbName = "test";
  $this->dbUsername = "username";
  $this->dbPassword = "userpassword";
 }
}
class test_query extends PHPDS_query
{
 protected $connector = "test_connector";
 protected $sql = "SHOW TABLES";
}
  $tables = $this->db->invokeQuery('test_query');

As you see, a query will use a different database connector if it’s provided in its field. Just give the class name of a connector class and the framework will handle the rest.

In this case, we used the “legacy” connector, which is the Mysql connector used all around by the framework. If you want to connect to another database software, you’ll have to write a custom connector (it’s not a big deal actually, just remapping the library function to class methods).

In a future version, you’ll be able to set the actual parameters in the configuration file, like the “master” database.

See How can I interact with a remote database in my own plugins?

Réaction

Était-ce utile?

Oui Non
Vous avez indiqué que ce sujet ne vous a pas été utile ...
Pouvez-vous SVP laisser un commentaire nous disant pourquoi? Merci!
Merci pour vos commentaires.

Laissez votre avis sur ce sujet.

Valider