Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function getData()
$data = ArrayHelper::fromObject($config);

// Get the correct driver at runtime
$data['dbtype'] = Factory::getDbo()->getName();
$data['dbtype'] = $this->getDbo()->getName();

// Prime the asset_id for the rules.
$data['asset_id'] = 1;
Expand Down
2 changes: 2 additions & 0 deletions libraries/src/Extension/Service/Provider/MVCFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Joomla\CMS\Form\FormFactoryInterface;
use Joomla\CMS\MVC\Factory\ApiMVCFactory;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\Database\DatabaseInterface;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Joomla\Event\DispatcherInterface;
Expand Down Expand Up @@ -71,6 +72,7 @@ function (Container $container)

$factory->setFormFactory($container->get(FormFactoryInterface::class));
$factory->setDispatcher($container->get(DispatcherInterface::class));
$factory->setDbo($container->get(DatabaseInterface::class));

return $factory;
}
Expand Down
29 changes: 17 additions & 12 deletions libraries/src/MVC/Factory/MVCFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Joomla\CMS\Factory;
use Joomla\CMS\Form\FormFactoryAwareInterface;
use Joomla\CMS\Form\FormFactoryAwareTrait;
use Joomla\CMS\MVC\Model\DatabaseAwareInterface;
use Joomla\CMS\MVC\Model\DatabaseAwareTrait;
use Joomla\CMS\MVC\Model\ModelInterface;
use Joomla\Event\DispatcherAwareInterface;
use Joomla\Event\DispatcherAwareTrait;
Expand All @@ -24,9 +26,9 @@
*
* @since 3.10.0
*/
class MVCFactory implements MVCFactoryInterface, FormFactoryAwareInterface, DispatcherAwareInterface
class MVCFactory implements MVCFactoryInterface, FormFactoryAwareInterface, DispatcherAwareInterface, DatabaseAwareInterface
{
use FormFactoryAwareTrait, DispatcherAwareTrait;
use FormFactoryAwareTrait, DispatcherAwareTrait, DatabaseAwareTrait;

/**
* The namespace to create the objects from.
Expand Down Expand Up @@ -125,6 +127,18 @@ public function createModel($name, $prefix = '', array $config = [])
$this->setFormFactoryOnObject($model);
$this->setDispatcherOnObject($model);

if ($model instanceof DatabaseAwareInterface)
{
try
{
$model->setDbo($this->getDbo());
}
catch (\UnexpectedValueException $e)
{
// Ignore it
}
}

return $model;
}

Expand Down Expand Up @@ -214,16 +228,7 @@ public function createTable($name, $prefix = '', array $config = [])
return null;
}

if (\array_key_exists('dbo', $config))
{
$db = $config['dbo'];
}
else
{
$db = Factory::getDbo();
}

return new $className($db);
return new $className(\array_key_exists('dbo', $config) ? $config['dbo'] : $this->getDbo());
}

/**
Expand Down
6 changes: 5 additions & 1 deletion libraries/src/MVC/Model/BaseDatabaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*
* @since 2.5.5
*/
abstract class BaseDatabaseModel extends BaseModel implements DatabaseModelInterface, DispatcherAwareInterface
abstract class BaseDatabaseModel extends BaseModel implements DatabaseModelInterface, DatabaseAwareInterface, DispatcherAwareInterface
{
use DatabaseAwareTrait, MVCFactoryAwareTrait, DispatcherAwareTrait;

Expand Down Expand Up @@ -82,6 +82,10 @@ public function __construct($config = array(), MVCFactoryInterface $factory = nu
$this->option = ComponentHelper::getComponentName($this, $r[1]);
}

/**
* @deprecated 5.0 Database instance is injected through the setter function,
* subclasses should not use the db instance in constructor anymore
*/
$this->setDbo(\array_key_exists('dbo', $config) ? $config['dbo'] : Factory::getDbo());

// Set the default view search path
Expand Down
32 changes: 32 additions & 0 deletions libraries/src/MVC/Model/DatabaseAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\MVC\Model;

\defined('_JEXEC') or die;

use Joomla\Database\DatabaseInterface;

/**
* Interface to be implemented by classes depending on a database.
*
* @since __DEPLOY_VERSION__
*/
interface DatabaseAwareInterface
{
/**
* Set the database to use.
*
* @param DatabaseInterface $db The database to use.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function setDbo(DatabaseInterface $db = null): void;
}
4 changes: 2 additions & 2 deletions libraries/src/MVC/Model/DatabaseAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ trait DatabaseAwareTrait
* @since 4.0.0
* @throws \UnexpectedValueException
*/
public function getDbo()
public function getDbo(): DatabaseInterface
{
if ($this->_db)
{
Expand All @@ -54,7 +54,7 @@ public function getDbo()
*
* @since 4.0.0
*/
public function setDbo(DatabaseInterface $db = null)
public function setDbo(DatabaseInterface $db = null): void
{
$this->_db = $db;
}
Expand Down