Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 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
58 changes: 31 additions & 27 deletions administrator/components/com_config/src/Model/ApplicationModel.php
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->getDatabase()->getName();

// Prime the asset_id for the rules.
$data['asset_id'] = 1;
Expand Down Expand Up @@ -502,11 +502,12 @@ public function save($data)
// Purge the database session table if we are changing to the database handler.
if ($prev['session_handler'] != 'database' && $data['session_handler'] == 'database')
{
$query = $this->_db->getQuery(true)
->delete($this->_db->quoteName('#__session'))
->where($this->_db->quoteName('time') . ' < ' . (time() - 1));
$this->_db->setQuery($query);
$this->_db->execute();
$db = $this->getDatabase();
$query = $db->getQuery(true)
->delete($db->quoteName('#__session'))
->where($db->quoteName('time') . ' < ' . (time() - 1));
$db->setQuery($query);
$db->execute();
}

// Purge the database session table if we are disabling session metadata
Expand Down Expand Up @@ -1170,16 +1171,19 @@ public function storePermissions($permission = null)

try
{
// The database instance
$db = $this->getDatabase();

// Get the asset id by the name of the component.
$query = $this->_db->getQuery(true)
->select($this->_db->quoteName('id'))
->from($this->_db->quoteName('#__assets'))
->where($this->_db->quoteName('name') . ' = :component')
$query = $db->getQuery(true)
->select($db->quoteName('id'))
->from($db->quoteName('#__assets'))
->where($db->quoteName('name') . ' = :component')
->bind(':component', $permission['component']);

$this->_db->setQuery($query);
$db->setQuery($query);

$assetId = (int) $this->_db->loadResult();
$assetId = (int) $db->loadResult();

// Fetch the parent asset id.
$parentAssetId = null;
Expand All @@ -1196,38 +1200,38 @@ public function storePermissions($permission = null)
{
// In this case we need to get the component rules too.
$query->clear()
->select($this->_db->quoteName('parent_id'))
->from($this->_db->quoteName('#__assets'))
->where($this->_db->quoteName('id') . ' = :assetid')
->select($db->quoteName('parent_id'))
->from($db->quoteName('#__assets'))
->where($db->quoteName('id') . ' = :assetid')
->bind(':assetid', $assetId, ParameterType::INTEGER);

$this->_db->setQuery($query);
$db->setQuery($query);

$parentAssetId = (int) $this->_db->loadResult();
$parentAssetId = (int) $db->loadResult();
}

// Get the group parent id of the current group.
$rule = (int) $permission['rule'];
$query->clear()
->select($this->_db->quoteName('parent_id'))
->from($this->_db->quoteName('#__usergroups'))
->where($this->_db->quoteName('id') . ' = :rule')
->select($db->quoteName('parent_id'))
->from($db->quoteName('#__usergroups'))
->where($db->quoteName('id') . ' = :rule')
->bind(':rule', $rule, ParameterType::INTEGER);

$this->_db->setQuery($query);
$db->setQuery($query);

$parentGroupId = (int) $this->_db->loadResult();
$parentGroupId = (int) $db->loadResult();

// Count the number of child groups of the current group.
$query->clear()
->select('COUNT(' . $this->_db->quoteName('id') . ')')
->from($this->_db->quoteName('#__usergroups'))
->where($this->_db->quoteName('parent_id') . ' = :rule')
->select('COUNT(' . $db->quoteName('id') . ')')
->from($db->quoteName('#__usergroups'))
->where($db->quoteName('parent_id') . ' = :rule')
->bind(':rule', $rule, ParameterType::INTEGER);

$this->_db->setQuery($query);
$db->setQuery($query);

$totalChildGroups = (int) $this->_db->loadResult();
$totalChildGroups = (int) $db->loadResult();
}
catch (\Exception $e)
{
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 @@ -14,6 +14,7 @@
use Joomla\CMS\MVC\Factory\ApiMVCFactory;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\Router\SiteRouter;
use Joomla\Database\DatabaseInterface;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Joomla\Event\DispatcherInterface;
Expand Down Expand Up @@ -72,6 +73,7 @@ function (Container $container)

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

return $factory;
Expand Down
28 changes: 23 additions & 5 deletions libraries/src/MVC/Factory/MVCFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
use Joomla\CMS\MVC\Model\ModelInterface;
use Joomla\CMS\Router\SiteRouterAwareInterface;
use Joomla\CMS\Router\SiteRouterAwareTrait;
use Joomla\Database\DatabaseAwareInterface;
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Database\DatabaseInterface;
use Joomla\Database\Exception\DatabaseNotFoundException;
use Joomla\Event\DispatcherAwareInterface;
use Joomla\Event\DispatcherAwareTrait;
use Joomla\Input\Input;
Expand All @@ -28,7 +32,7 @@
*/
class MVCFactory implements MVCFactoryInterface, FormFactoryAwareInterface, SiteRouterAwareInterface
{
use FormFactoryAwareTrait, DispatcherAwareTrait, SiteRouterAwareTrait;
use FormFactoryAwareTrait, DispatcherAwareTrait, DatabaseAwareTrait, SiteRouterAwareTrait;

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

if ($model instanceof DatabaseAwareInterface)
{
try
{
$model->setDatabase($this->getDatabase());
}
catch (DatabaseNotFoundException $e)
{
@trigger_error(sprintf('Database must be set, this will not be catched anymore in 5.0.'), E_USER_DEPRECATED);
$model->setDatabase(Factory::getContainer()->get(DatabaseInterface::class));
}
}

return $model;
}

Expand Down Expand Up @@ -219,13 +236,14 @@ public function createTable($name, $prefix = '', array $config = [])
return null;
}

if (\array_key_exists('dbo', $config))
try
{
$db = $config['dbo'];
$db = \array_key_exists('dbo', $config) ? $config['dbo'] : $this->getDatabase();
}
else
catch (DatabaseNotFoundException $e)
{
$db = Factory::getDbo();
@trigger_error(sprintf('Database must be set, this will not be catched anymore in 5.0.'), E_USER_DEPRECATED);
$db = Factory::getContainer()->get(DatabaseInterface::class);
}

return new $className($db);
Expand Down
82 changes: 80 additions & 2 deletions libraries/src/MVC/Model/BaseDatabaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\MVC\Factory\MVCFactoryServiceInterface;
use Joomla\CMS\Table\Table;
use Joomla\Database\DatabaseAwareInterface;
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Database\DatabaseInterface;
use Joomla\Database\DatabaseQuery;
use Joomla\Database\Exception\DatabaseNotFoundException;
use Joomla\Event\DispatcherAwareInterface;
use Joomla\Event\DispatcherAwareTrait;
use Joomla\Event\DispatcherInterface;
Expand All @@ -36,7 +40,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,7 +86,17 @@ public function __construct($config = array(), MVCFactoryInterface $factory = nu
$this->option = ComponentHelper::getComponentName($this, $r[1]);
}

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

if ($db)
{
@trigger_error(sprintf('Database is not available in constructor in 5.0.'), E_USER_DEPRECATED);
$this->setDatabase($db);
}

// Set the default view search path
if (\array_key_exists('table_path', $config))
Expand Down Expand Up @@ -339,4 +353,68 @@ protected function dispatchEvent(EventInterface $event)
Factory::getContainer()->get(DispatcherInterface::class)->dispatch($event->getName(), $event);
}
}

/**
* Get the database driver.
*
* @return DatabaseInterface The database driver.
*
* @since __DEPLOY_VERSION__
* @throws \UnexpectedValueException
*
* @deprecated 5.0 Use getDatabase() instead
*/
public function getDbo(): DatabaseInterface
{
try
{
return $this->getDatabase();
}
catch (DatabaseNotFoundException $e)
{
throw new \UnexpectedValueException('Database driver not set in ' . __CLASS__);
}
}

/**
* Set the database driver.
*
* @param DatabaseInterface $db The database driver.
*
* @return void
*
* @since __DEPLOY_VERSION__
*
* @deprecated 5.0 Use setDatabase() instead
*/
public function setDbo(DatabaseInterface $db = null): void
{
if ($db === null)
{
return;
}

$this->setDatabase($db);
}

/**
* Proxy for _db variable.
*
* @param string $name The name of the element
*
* @return mixed The value of the element if set, null otherwise
*
* @since __DEPLOY_VERSION__
*
* @deprecated 5.0 Use getDatabase() instead of directly accessing _db
*/
public function __get($name)
{
if ($name === '_db')
{
return $this->getDatabase();
}

return $this->$name;
}
}
8 changes: 8 additions & 0 deletions libraries/src/MVC/Model/DatabaseAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* Database aware trait.
*
* @since 4.0.0
*
* @deprecated 5.0 Use the trait from the database package
*/
trait DatabaseAwareTrait
{
Expand All @@ -24,6 +26,8 @@ trait DatabaseAwareTrait
*
* @var DatabaseInterface
* @since 4.0.0
*
* @deprecated 5.0 Use the trait from the database package
*/
protected $_db;

Expand All @@ -34,6 +38,8 @@ trait DatabaseAwareTrait
*
* @since 4.0.0
* @throws \UnexpectedValueException
*
* @deprecated 5.0 Use the trait from the database package
*/
public function getDbo()
{
Expand All @@ -53,6 +59,8 @@ public function getDbo()
* @return void
*
* @since 4.0.0
*
* @deprecated 5.0 Use the trait from the database package
*/
public function setDbo(DatabaseInterface $db = null)
{
Expand Down