JFactory has a getDbo method, but no setDbo. Why? It can be useful for unit-testing to be able to overwrite the global database driver. Any other way to do this?

EDIT: $database is a public property of JFactory, so I can just overwrite it like so:

$myDatabase = new JDatabaseDriver ...
JFactory::$database = $myDatabase;

1 Answer 1


You can set the database driver, just not with any simple function (that I know of), such as setDbo.

If you look at the Joomla Database Framework package:

use Joomla\Database;

class MyApplication extends AbstractApplication
    protected $db;

    protected function initialise()
        // Make the database driver.
        $dbFactory = new Database\DatabaseFactory;

        $this->db = $dbFactory->getDriver(
                'host' => $this->get('database.host'),
                'user' => $this->get('database.user'),
                'password' => $this->get('database.password'),
                'port' => $this->get('database.port'),
                'socket' => $this->get('database.socket'),
                'database' => $this->get('database.name'),

So you can replace $this->get('database.driver') with the driver that you need.

  • Not possible in my case, since all methods in my class is static (helper class that's is called from all manner of places). But it seems that JFactory::$database is a public property, so I can just overwrite it. Jul 31, 2017 at 11:57
  • Well, of course it's possible, but such code would better be placed in a constructor, of which I never call. So. Jul 31, 2017 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.