2

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;
2

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(
            $this->get('database.driver'),
            array(
                '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. – Olle Härstedt Jul 31 '17 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. – Olle Härstedt Jul 31 '17 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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