Skip to content
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

[5.0] Improve constructors of JTable objects #40129

Merged
merged 9 commits into from
Sep 4, 2023
17 changes: 8 additions & 9 deletions administrator/components/com_banners/src/Table/BannerTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Joomla\CMS\Versioning\VersionableTableInterface;
use Joomla\Database\DatabaseDriver;
use Joomla\Database\ParameterType;
use Joomla\Event\DispatcherInterface;
use Joomla\Registry\Registry;
use Joomla\Utilities\ArrayHelper;

Expand All @@ -43,15 +44,16 @@ class BannerTable extends Table implements VersionableTableInterface
/**
* Constructor
*
* @param DatabaseDriver $db Database connector object
* @param DatabaseDriver $db Database connector object
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
*
* @since 1.5
*/
public function __construct(DatabaseDriver $db)
public function __construct(DatabaseDriver $db, DispatcherInterface $dispatcher = null)
{
$this->typeAlias = 'com_banners.banner';

parent::__construct('#__banners', 'id', $db);
parent::__construct('#__banners', 'id', $db, $dispatcher);

$this->created = Factory::getDate()->toSql();
$this->setColumnAlias('published', 'state');
Expand Down Expand Up @@ -252,16 +254,14 @@ public function store($updateNulls = true)
parent::store($updateNulls);
} else {
// Get the old row
/** @var BannerTable $oldrow */
$oldrow = Table::getInstance('BannerTable', __NAMESPACE__ . '\\', ['dbo' => $db]);
$oldrow = new self($db, $this->getDispatcher());

if (!$oldrow->load($this->id) && $oldrow->getError()) {
$this->setError($oldrow->getError());
}

// Verify that the alias is unique
/** @var BannerTable $table */
$table = Table::getInstance('BannerTable', __NAMESPACE__ . '\\', ['dbo' => $db]);
$table = new self($db, $this->getDispatcher());

if ($table->load(['alias' => $this->alias, 'catid' => $this->catid]) && ($table->id != $this->id || $this->id == 0)) {
$this->setError(Text::_('COM_BANNERS_ERROR_UNIQUE_ALIAS'));
Expand Down Expand Up @@ -317,8 +317,7 @@ public function stick($pks = null, $state = 1, $userId = 0)
}

// Get an instance of the table
/** @var BannerTable $table */
$table = Table::getInstance('BannerTable', __NAMESPACE__ . '\\', ['dbo' => $this->_db]);
$table = new self($this->getDbo(), $this->getDispatcher());

// For all keys
foreach ($pks as $pk) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Joomla\CMS\Table\Table;
use Joomla\CMS\Versioning\VersionableTableInterface;
use Joomla\Database\DatabaseDriver;
use Joomla\Event\DispatcherInterface;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
Expand All @@ -37,17 +38,18 @@ class ClientTable extends Table implements VersionableTableInterface
/**
* Constructor
*
* @param DatabaseDriver $db Database connector object
* @param DatabaseDriver $db Database connector object
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
*
* @since 1.5
*/
public function __construct(DatabaseDriver $db)
public function __construct(DatabaseDriver $db, DispatcherInterface $dispatcher = null)
{
$this->typeAlias = 'com_banners.client';

$this->setColumnAlias('published', 'state');

parent::__construct('#__banner_clients', 'id', $db);
parent::__construct('#__banner_clients', 'id', $db, $dispatcher);
}

/**
Expand Down
12 changes: 7 additions & 5 deletions administrator/components/com_contact/src/Table/ContactTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Joomla\CMS\User\CurrentUserTrait;
use Joomla\CMS\Versioning\VersionableTableInterface;
use Joomla\Database\DatabaseDriver;
use Joomla\Event\DispatcherInterface;
use Joomla\String\StringHelper;

// phpcs:disable PSR1.Files.SideEffects
Expand Down Expand Up @@ -57,15 +58,16 @@ class ContactTable extends Table implements VersionableTableInterface, TaggableT
/**
* Constructor
*
* @param DatabaseDriver $db Database connector object
* @param DatabaseDriver $db Database connector object
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
*
* @since 1.0
*/
public function __construct(DatabaseDriver $db)
public function __construct(DatabaseDriver $db, DispatcherInterface $dispatcher = null)
{
$this->typeAlias = 'com_contact.contact';

parent::__construct('#__contact_details', 'id', $db);
parent::__construct('#__contact_details', 'id', $db, $dispatcher);

$this->setColumnAlias('title', 'name');
}
Expand Down Expand Up @@ -119,7 +121,7 @@ public function store($updateNulls = true)
}

// Verify that the alias is unique
$table = Table::getInstance('ContactTable', __NAMESPACE__ . '\\', ['dbo' => $this->getDbo()]);
$table = new self($this->getDbo(), $this->getDispatcher());

if ($table->load(['alias' => $this->alias, 'catid' => $this->catid]) && ($table->id != $this->id || $this->id == 0)) {
// Is the existing contact trashed?
Expand Down Expand Up @@ -255,7 +257,7 @@ public function generateAlias()


/**
* Get the type alias for the history table
* Get the type alias for the history and tags mapping table
*
* @return string The alias as described above
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Joomla\CMS\Table\Table;
use Joomla\Database\DatabaseDriver;
use Joomla\Event\DispatcherInterface;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
Expand All @@ -27,12 +28,13 @@ class FeaturedTable extends Table
/**
* Constructor
*
* @param DatabaseDriver $db Database connector object
* @param DatabaseDriver $db Database connector object
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
*
* @since 1.6
*/
public function __construct(DatabaseDriver $db)
public function __construct(DatabaseDriver $db, DispatcherInterface $dispatcher = null)
{
parent::__construct('#__content_frontpage', 'content_id', $db);
parent::__construct('#__content_frontpage', 'content_id', $db, $dispatcher);
}
}
10 changes: 6 additions & 4 deletions administrator/components/com_fields/src/Table/FieldTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Joomla\CMS\User\CurrentUserInterface;
use Joomla\CMS\User\CurrentUserTrait;
use Joomla\Database\DatabaseDriver;
use Joomla\Event\DispatcherInterface;
use Joomla\Registry\Registry;
use Joomla\String\StringHelper;

Expand Down Expand Up @@ -45,13 +46,14 @@ class FieldTable extends Table implements CurrentUserInterface
/**
* Class constructor.
*
* @param DatabaseDriver $db DatabaseDriver object.
* @param DatabaseDriver $db Database connector object
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
*
* @since 3.7.0
*/
public function __construct($db = null)
public function __construct(DatabaseDriver $db, DispatcherInterface $dispatcher = null)
{
parent::__construct('#__fields', 'id', $db);
parent::__construct('#__fields', 'id', $db, $dispatcher);

$this->setColumnAlias('published', 'state');
}
Expand Down Expand Up @@ -150,7 +152,7 @@ public function check()
$this->name = str_replace(',', '-', $this->name);

// Verify that the name is unique
$table = new static($this->_db);
$table = new self($this->_db, $this->getDispatcher());

if ($table->load(['name' => $this->name]) && ($table->id != $this->id || $this->id == 0)) {
$this->setError(Text::_('COM_FIELDS_ERROR_UNIQUE_NAME'));
Expand Down
8 changes: 5 additions & 3 deletions administrator/components/com_fields/src/Table/GroupTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Joomla\CMS\User\CurrentUserInterface;
use Joomla\CMS\User\CurrentUserTrait;
use Joomla\Database\DatabaseDriver;
use Joomla\Event\DispatcherInterface;
use Joomla\Registry\Registry;

// phpcs:disable PSR1.Files.SideEffects
Expand All @@ -43,13 +44,14 @@ class GroupTable extends Table implements CurrentUserInterface
/**
* Class constructor.
*
* @param DatabaseDriver $db DatabaseDriver object.
* @param DatabaseDriver $db Database connector object
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
*
* @since 3.7.0
*/
public function __construct($db = null)
public function __construct(DatabaseDriver $db, DispatcherInterface $dispatcher = null)
{
parent::__construct('#__fields_groups', 'id', $db);
parent::__construct('#__fields_groups', 'id', $db, $dispatcher);

$this->setColumnAlias('published', 'state');
}
Expand Down
10 changes: 6 additions & 4 deletions administrator/components/com_finder/src/Table/FilterTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Joomla\CMS\User\CurrentUserInterface;
use Joomla\CMS\User\CurrentUserTrait;
use Joomla\Database\DatabaseDriver;
use Joomla\Event\DispatcherInterface;
use Joomla\Registry\Registry;

// phpcs:disable PSR1.Files.SideEffects
Expand Down Expand Up @@ -51,13 +52,14 @@ class FilterTable extends Table implements CurrentUserInterface
/**
* Constructor
*
* @param DatabaseDriver $db Database Driver connector object.
* @param DatabaseDriver $db Database connector object
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
*
* @since 2.5
*/
public function __construct(DatabaseDriver $db)
public function __construct(DatabaseDriver $db, DispatcherInterface $dispatcher = null)
{
parent::__construct('#__finder_filters', 'filter_id', $db);
parent::__construct('#__finder_filters', 'filter_id', $db, $dispatcher);

$this->setColumnAlias('published', 'state');
}
Expand Down Expand Up @@ -158,7 +160,7 @@ public function store($updateNulls = true)
}

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

if ($table->load(['alias' => $this->alias]) && ($table->filter_id != $this->filter_id || $this->filter_id == 0)) {
$this->setError(Text::_('COM_FINDER_FILTER_ERROR_UNIQUE_ALIAS'));
Expand Down
8 changes: 5 additions & 3 deletions administrator/components/com_finder/src/Table/LinkTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Joomla\CMS\Table\Table;
use Joomla\Database\DatabaseDriver;
use Joomla\Event\DispatcherInterface;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
Expand All @@ -35,13 +36,14 @@ class LinkTable extends Table
/**
* Constructor
*
* @param DatabaseDriver $db Database Driver connector object.
* @param DatabaseDriver $db Database connector object
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
*
* @since 2.5
*/
public function __construct(DatabaseDriver $db)
public function __construct(DatabaseDriver $db, DispatcherInterface $dispatcher = null)
{
parent::__construct('#__finder_links', 'link_id', $db);
parent::__construct('#__finder_links', 'link_id', $db, $dispatcher);
}

/**
Expand Down
8 changes: 5 additions & 3 deletions administrator/components/com_finder/src/Table/MapTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Joomla\CMS\Language\Text;
use Joomla\CMS\Table\Nested;
use Joomla\Database\DatabaseDriver;
use Joomla\Event\DispatcherInterface;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
Expand All @@ -30,13 +31,14 @@ class MapTable extends Nested
/**
* Constructor
*
* @param DatabaseDriver $db Database Driver connector object.
* @param DatabaseDriver $db Database connector object
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
*
* @since 2.5
*/
public function __construct(DatabaseDriver $db)
public function __construct(DatabaseDriver $db, DispatcherInterface $dispatcher = null)
{
parent::__construct('#__finder_taxonomy', 'id', $db);
parent::__construct('#__finder_taxonomy', 'id', $db, $dispatcher);

$this->setColumnAlias('published', 'state');
$this->access = (int) Factory::getApplication()->get('access');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Joomla\CMS\User\CurrentUserInterface;
use Joomla\CMS\User\CurrentUserTrait;
use Joomla\Database\DatabaseDriver;
use Joomla\Event\DispatcherInterface;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
Expand All @@ -40,13 +41,14 @@ class StepTable extends Table implements CurrentUserInterface
/**
* Constructor
*
* @param DatabaseDriver $db Database connector object
* @param DatabaseDriver $db Database connector object
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
*
* @since 4.3.0
*/
public function __construct(DatabaseDriver $db)
public function __construct(DatabaseDriver $db, DispatcherInterface $dispatcher = null)
{
parent::__construct('#__guidedtour_steps', 'id', $db);
parent::__construct('#__guidedtour_steps', 'id', $db, $dispatcher);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Joomla\CMS\User\CurrentUserInterface;
use Joomla\CMS\User\CurrentUserTrait;
use Joomla\Database\DatabaseDriver;
use Joomla\Event\DispatcherInterface;
use Joomla\String\StringHelper;

// phpcs:disable PSR1.Files.SideEffects
Expand Down Expand Up @@ -51,13 +52,14 @@ class TourTable extends Table implements CurrentUserInterface
/**
* Constructor
*
* @param DatabaseDriver $db Database connector object
* @param DatabaseDriver $db Database connector object
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
*
* @since 4.3.0
*/
public function __construct(DatabaseDriver $db)
public function __construct(DatabaseDriver $db, DispatcherInterface $dispatcher = null)
{
parent::__construct('#__guidedtours', 'id', $db);
parent::__construct('#__guidedtours', 'id', $db, $dispatcher);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Joomla\CMS\Table\Table;
use Joomla\Database\DatabaseDriver;
use Joomla\Event\DispatcherInterface;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
Expand All @@ -36,14 +37,15 @@ class UpdatesiteTable extends Table
/**
* Constructor
*
* @param DatabaseDriver $db Database connector object
* @param DatabaseDriver $db Database connector object
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
*
* @since 4.0.0
*/
public function __construct(DatabaseDriver $db)
public function __construct(DatabaseDriver $db, DispatcherInterface $dispatcher = null)
{
$this->typeAlias = 'com_installer.downloadkey';

parent::__construct('#__update_sites', 'update_site_id', $db);
parent::__construct('#__update_sites', 'update_site_id', $db, $dispatcher);
}
}
Loading