Skip to content

[5.4] Refactor CMS Table classes #45243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 13 additions & 11 deletions libraries/src/Table/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace Joomla\CMS\Table;

use Joomla\CMS\Language\Text;
use Joomla\Database\DatabaseDriver;
use Joomla\Database\DatabaseInterface;
use Joomla\Event\DispatcherInterface;

// phpcs:disable PSR1.Files.SideEffects
Expand Down Expand Up @@ -59,12 +59,12 @@ class Asset extends Nested
/**
* Constructor
*
* @param DatabaseDriver $db Database connector object
* @param DatabaseInterface $db Database connector object
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
*
* @since 1.7.0
*/
public function __construct(DatabaseDriver $db, ?DispatcherInterface $dispatcher = null)
public function __construct(DatabaseInterface $db, ?DispatcherInterface $dispatcher = null)
{
parent::__construct('#__assets', 'id', $db, $dispatcher);
}
Expand Down Expand Up @@ -109,14 +109,15 @@ public function check()
// Nested does not allow parent_id = 0, override this.
if ($this->parent_id > 0) {
// Get the DatabaseQuery object
$query = $this->_db->getQuery(true)
$db = $this->getDatabase();
$query = $db->getQuery(true)
->select('1')
->from($this->_db->quoteName($this->_tbl))
->where($this->_db->quoteName('id') . ' = ' . $this->parent_id);
->from($db->quoteName($this->_tbl))
->where($db->quoteName('id') . ' = ' . $this->parent_id);

$query->setLimit(1);

if ($this->_db->setQuery($query)->loadResult()) {
if ($db->setQuery($query)->loadResult()) {
return true;
}

Expand Down Expand Up @@ -153,7 +154,8 @@ public function rebuild($parentId = null, $leftId = 0, $level = 0, $path = null)
}
}

$query = $this->_db->getQuery(true);
$db = $this->getDatabase();
$query = $db->getQuery(true);

// Build the structure of the recursive query.
if (!isset($this->_cache['rebuild.sql'])) {
Expand All @@ -175,9 +177,9 @@ public function rebuild($parentId = null, $leftId = 0, $level = 0, $path = null)
// Make a shortcut to database object.

// Assemble the query to find all children of this node.
$this->_db->setQuery(\sprintf($this->_cache['rebuild.sql'], (int) $parentId));
$db->setQuery(\sprintf($this->_cache['rebuild.sql'], (int) $parentId));

$children = $this->_db->loadObjectList();
$children = $db->loadObjectList();

// The right value of this node is the left value + 1
$rightId = $leftId + 1;
Expand Down Expand Up @@ -205,7 +207,7 @@ public function rebuild($parentId = null, $leftId = 0, $level = 0, $path = null)
->set('rgt = ' . (int) $rightId)
->set('level = ' . (int) $level)
->where($this->_tbl_key . ' = ' . (int) $parentId);
$this->_db->setQuery($query)->execute();
$db->setQuery($query)->execute();

// Return the right value of this node + 1.
return $rightId + 1;
Expand Down
34 changes: 18 additions & 16 deletions libraries/src/Table/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Joomla\CMS\User\CurrentUserInterface;
use Joomla\CMS\User\CurrentUserTrait;
use Joomla\CMS\Versioning\VersionableTableInterface;
use Joomla\Database\DatabaseDriver;
use Joomla\Database\DatabaseInterface;
use Joomla\Database\ParameterType;
use Joomla\Event\DispatcherInterface;
use Joomla\Registry\Registry;
Expand Down Expand Up @@ -48,12 +48,12 @@ class Category extends Nested implements VersionableTableInterface, TaggableTabl
/**
* Constructor
*
* @param DatabaseDriver $db Database connector object
* @param DatabaseInterface $db Database connector object
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
*
* @since 1.5
*/
public function __construct(DatabaseDriver $db, ?DispatcherInterface $dispatcher = null)
public function __construct(DatabaseInterface $db, ?DispatcherInterface $dispatcher = null)
{
/**
* @deprecated 4.0 will be removed in 6.0
Expand Down Expand Up @@ -108,33 +108,35 @@ protected function _getAssetParentId(?Table $table = null, $id = null)
$assetId = null;

// This is a category under a category.
$db = $this->getDatabase();

if ($this->parent_id > 1) {
// Build the query to get the asset id for the parent category.
$query = $this->_db->getQuery(true)
->select($this->_db->quoteName('asset_id'))
->from($this->_db->quoteName('#__categories'))
->where($this->_db->quoteName('id') . ' = :parentId')
$query = $db->getQuery(true)
->select($db->quoteName('asset_id'))
->from($db->quoteName('#__categories'))
->where($db->quoteName('id') . ' = :parentId')
->bind(':parentId', $this->parent_id, ParameterType::INTEGER);

// Get the asset id from the database.
$this->_db->setQuery($query);
$db->setQuery($query);

if ($result = $this->_db->loadResult()) {
if ($result = $db->loadResult()) {
$assetId = (int) $result;
}
} elseif ($assetId === null) {
// This is a category that needs to parent with the extension.
// Build the query to get the asset id for the parent category.
$query = $this->_db->getQuery(true)
->select($this->_db->quoteName('id'))
->from($this->_db->quoteName('#__assets'))
->where($this->_db->quoteName('name') . ' = :extension')
$query = $db->getQuery(true)
->select($db->quoteName('id'))
->from($db->quoteName('#__assets'))
->where($db->quoteName('name') . ' = :extension')
->bind(':extension', $this->extension);

// Get the asset id from the database.
$this->_db->setQuery($query);
$db->setQuery($query);

if ($result = $this->_db->loadResult()) {
if ($result = $db->loadResult()) {
$assetId = (int) $result;
}
}
Expand Down Expand Up @@ -259,7 +261,7 @@ public function store($updateNulls = true)
}

// Verify that the alias is unique
$table = new Category($this->getDbo(), $this->getDispatcher());
$table = new Category($this->getDatabase(), $this->getDispatcher());

if (
$table->load(['alias' => $this->alias, 'parent_id' => (int) $this->parent_id, 'extension' => $this->extension])
Expand Down
21 changes: 11 additions & 10 deletions libraries/src/Table/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Joomla\CMS\User\CurrentUserInterface;
use Joomla\CMS\User\CurrentUserTrait;
use Joomla\CMS\Versioning\VersionableTableInterface;
use Joomla\Database\DatabaseDriver;
use Joomla\Database\DatabaseInterface;
use Joomla\Database\ParameterType;
use Joomla\Event\DispatcherInterface;
use Joomla\Registry\Registry;
Expand Down Expand Up @@ -49,12 +49,12 @@ class Content extends Table implements VersionableTableInterface, TaggableTableI
/**
* Constructor
*
* @param DatabaseDriver $db Database connector object
* @param DatabaseInterface $db Database connector object
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
*
* @since 1.5
*/
public function __construct(DatabaseDriver $db, ?DispatcherInterface $dispatcher = null)
public function __construct(DatabaseInterface $db, ?DispatcherInterface $dispatcher = null)
{
$this->typeAlias = 'com_content.article';

Expand Down Expand Up @@ -111,16 +111,17 @@ protected function _getAssetParentId(?Table $table = null, $id = null)
$catId = (int) $this->catid;

// Build the query to get the asset id for the parent category.
$query = $this->_db->getQuery(true)
->select($this->_db->quoteName('asset_id'))
->from($this->_db->quoteName('#__categories'))
->where($this->_db->quoteName('id') . ' = :catid')
$db = $this->getDatabase();
$query = $db->getQuery(true)
->select($db->quoteName('asset_id'))
->from($db->quoteName('#__categories'))
->where($db->quoteName('id') . ' = :catid')
->bind(':catid', $catId, ParameterType::INTEGER);

// Get the asset id from the database.
$this->_db->setQuery($query);
$db->setQuery($query);

if ($result = $this->_db->loadResult()) {
if ($result = $db->loadResult()) {
$assetId = (int) $result;
}
}
Expand Down Expand Up @@ -351,7 +352,7 @@ public function store($updateNulls = true)
}

// Verify that the alias is unique
$table = new self($this->getDbo(), $this->getDispatcher());
$table = new self($this->getDatabase(), $this->getDispatcher());

if ($table->load(['alias' => $this->alias, 'catid' => $this->catid]) && ($table->id != $this->id || $this->id == 0)) {
// Is the existing article trashed?
Expand Down
12 changes: 6 additions & 6 deletions libraries/src/Table/ContentHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Joomla\CMS\Factory;
use Joomla\CMS\User\CurrentUserInterface;
use Joomla\CMS\User\CurrentUserTrait;
use Joomla\Database\DatabaseDriver;
use Joomla\Database\DatabaseInterface;
use Joomla\Database\ParameterType;
use Joomla\Event\DispatcherInterface;

Expand Down Expand Up @@ -51,12 +51,12 @@ class ContentHistory extends Table implements CurrentUserInterface
/**
* Constructor
*
* @param DatabaseDriver $db Database connector object
* @param DatabaseInterface $db Database connector object
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
*
* @since 3.1
*/
public function __construct(DatabaseDriver $db, ?DispatcherInterface $dispatcher = null)
public function __construct(DatabaseInterface $db, ?DispatcherInterface $dispatcher = null)
{
parent::__construct('#__history', 'version_id', $db, $dispatcher);
$this->ignoreChanges = [
Expand Down Expand Up @@ -85,7 +85,7 @@ public function __construct(DatabaseDriver $db, ?DispatcherInterface $dispatcher
public function store($updateNulls = false)
{
$this->character_count = \strlen($this->version_data);
$typeTable = new ContentType($this->getDbo(), $this->getDispatcher());
$typeTable = new ContentType($this->getDatabase(), $this->getDispatcher());
$typeAlias = explode('.', $this->item_id);
array_pop($typeAlias);
$typeTable->load(['type_alias' => implode('.', $typeAlias)]);
Expand Down Expand Up @@ -166,7 +166,7 @@ public function getSha1($jsonData, ContentType $typeTable)
*/
public function getHashMatch()
{
$db = $this->_db;
$db = $this->getDatabase();
$itemId = $this->item_id;
$sha1Hash = $this->sha1_hash;
$query = $db->getQuery(true);
Expand Down Expand Up @@ -197,7 +197,7 @@ public function deleteOldVersions($maxVersions)
$result = true;

// Get the list of version_id values we want to save
$db = $this->_db;
$db = $this->getDatabase();
$itemId = $this->item_id;
$query = $db->getQuery(true);
$query->select($db->quoteName('version_id'))
Expand Down
10 changes: 5 additions & 5 deletions libraries/src/Table/ContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace Joomla\CMS\Table;

use Joomla\CMS\Language\Text;
use Joomla\Database\DatabaseDriver;
use Joomla\Database\DatabaseInterface;
use Joomla\Event\DispatcherInterface;

// phpcs:disable PSR1.Files.SideEffects
Expand All @@ -27,12 +27,12 @@ class ContentType extends Table
/**
* Constructor
*
* @param DatabaseDriver $db Database connector object
* @param DatabaseInterface $db Database connector object
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
*
* @since 3.1
*/
public function __construct(DatabaseDriver $db, ?DispatcherInterface $dispatcher = null)
public function __construct(DatabaseInterface $db, ?DispatcherInterface $dispatcher = null)
{
parent::__construct('#__content_types', 'type_id', $db, $dispatcher);
}
Expand Down Expand Up @@ -81,7 +81,7 @@ public function check()
public function store($updateNulls = false)
{
// Verify that the alias is unique
$table = new self($this->getDbo(), $this->getDispatcher());
$table = new self($this->getDatabase(), $this->getDispatcher());

if ($table->load(['type_alias' => $this->type_alias]) && ($table->type_id != $this->type_id || $this->type_id == 0)) {
$this->setError(Text::_('COM_TAGS_ERROR_UNIQUE_ALIAS'));
Expand Down Expand Up @@ -117,7 +117,7 @@ public function fieldmapExpand($assoc = true)
*/
public function getTypeId($typeAlias)
{
$db = $this->getDbo();
$db = $this->getDatabase();
$query = $db->getQuery(true);
$query->select($db->quoteName('type_id'))
->from($db->quoteName($this->_tbl))
Expand Down
14 changes: 7 additions & 7 deletions libraries/src/Table/CoreContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Joomla\CMS\Language\Text;
use Joomla\CMS\User\CurrentUserInterface;
use Joomla\CMS\User\CurrentUserTrait;
use Joomla\Database\DatabaseDriver;
use Joomla\Database\DatabaseInterface;
use Joomla\Database\ParameterType;
use Joomla\Event\DispatcherInterface;
use Joomla\String\StringHelper;
Expand Down Expand Up @@ -52,12 +52,12 @@ class CoreContent extends Table implements CurrentUserInterface
/**
* Constructor
*
* @param DatabaseDriver $db Database connector object
* @param DatabaseInterface $db Database connector object
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
*
* @since 3.1
*/
public function __construct(DatabaseDriver $db, ?DispatcherInterface $dispatcher = null)
public function __construct(DatabaseInterface $db, ?DispatcherInterface $dispatcher = null)
{
parent::__construct('#__ucm_content', 'core_content_id', $db, $dispatcher);

Expand Down Expand Up @@ -116,7 +116,7 @@ public function check()
$this->core_publish_up !== null
&& $this->core_publish_down !== null
&& $this->core_publish_down < $this->core_publish_up
&& $this->core_publish_down > $this->_db->getNullDate()
&& $this->core_publish_down > $this->getDatabase()->getNullDate()
) {
// Swap the dates.
$temp = $this->core_publish_up;
Expand Down Expand Up @@ -166,7 +166,7 @@ public function check()
*/
public function delete($pk = null)
{
$baseTable = new Ucm($this->getDbo(), $this->getDispatcher());
$baseTable = new Ucm($this->getDatabase(), $this->getDispatcher());

return parent::delete($pk) && $baseTable->delete($pk);
}
Expand Down Expand Up @@ -194,7 +194,7 @@ public function deleteByContentId($contentItemId = null, $typeAlias = null)
throw new \UnexpectedValueException('Null type alias not allowed.');
}

$db = $this->getDbo();
$db = $this->getDatabase();
$query = $db->getQuery(true);
$query->select($db->quoteName('core_content_id'))
->from($db->quoteName('#__ucm_content'))
Expand Down Expand Up @@ -281,7 +281,7 @@ public function store($updateNulls = true)
protected function storeUcmBase($updateNulls = true, $isNew = false)
{
// Store the ucm_base row
$db = $this->getDbo();
$db = $this->getDatabase();
$query = $db->getQuery(true);
$languageId = ContentHelper::getLanguageId($this->core_language);

Expand Down
Loading