I have a need to connect to a data source on an old unix box via ODBC, but I don't seem to be getting far and I can't find any examples to help me.
I have started with https://docs.joomla.org/Connecting_to_an_external_database and set up the following details:
$option['driver'] = 'pdo'; // Database driver name
$option['host'] = 'nnn.nnn.nnn.nnn:nnnn'; // Database host name
$options['dsn'] = 'Fred';
$option['user'] = 'Barney'; // User for database authentication
$option['password'] = 'Wilma'; // Password for database authentication
$option['database'] = 'dbname'; // Database name
$option['prefix'] = ''; // Database prefix (may be empty)
$dbx = JDatabaseDriver::getInstance($option);
With that configuration, I get an error message of:
Cannot instantiate abstract class JDatabaseDriverPdo E:\www\bedrock\libraries\joomla\database\driver.php:313
This happens because driver.php
takes the value 'pdo' from $option['driver'] = 'pdo';
and appends it to a class of JDatabaseDriver to be found in \libraries\database\driver\pdo.php
which is an abstract class the extends JDatabaseDriver
hence the message. It is the only abstract class in /driver
. If I replace 'pdo' with 'odbc', I get a message to say it can't load the database driver which will be because the class JDatabaseDriverOdbc is not found.
When I look at \libraries\database\driver\pdo.php
, I can see that 'odbc' is the default if no other driver is specified and further down I can see the connection string set up for 'odbc' so it looks like it is supported.
public function __construct($options)
{
// Get some basic values from the options.
$options['driver'] = (isset($options['driver'])) ? $options['driver'] : 'odbc';
$options['dsn'] = (isset($options['dsn'])) ? $options['dsn'] : '';
$options['host'] = (isset($options['host'])) ? $options['host'] : 'localhost';
...
// Find the correct PDO DSN Format to use:
switch ($this->options['driver'])
{
..
case 'odbc':
$format = 'odbc:DSN=#DSN#;UID:#USER#;PWD=#PASSWORD#';
$replace = array('#DSN#', '#USER#', '#PASSWORD#');
$with = array($this->options['dsn'], $this->options['user'], $this->options['password']);
It seems to me that $option['driver'] =
has two different uses depending on where it is used, as either a suffix to build a class name or as a switch to set the connection string.
I have also tried to use the class directly with
`$option['driver'] = 'odbc';
...
$dbx = JDatabaseDriverPdo::getInstance($option);
however that still tries to find a class of JDatabaseDriverOdbc
from driver.php
I really could use some pointers in the right direction or an example of ODBC in use.
Some hours later...
Looking at the code in driver.php and some of the data base formats in \libraries\database\driver\
it would appear I need to create a new class that extends JDatabaseDriverPdo, eg. class JDatabaseDriverOdbc extends JDatabaseDriverPdo
and make that available to my code. So I think the Joomla answer is yes you can do it if you write your own connector.
$option['driver'] = 'odbc'
can you try$option['driver'] = null;
? This will fail allisset()
checks that the core will run. I am at work at the moment and cannot go digging through the core code now.