Skip to content
Closed
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
47 changes: 29 additions & 18 deletions libraries/src/MVC/Model/BaseDatabaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

// Disable Underscore, because we need $_db for backward compatibility
// phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore

namespace Joomla\CMS\MVC\Model;

use Joomla\CMS\Cache\CacheControllerFactoryAwareInterface;
Expand Down Expand Up @@ -48,7 +51,9 @@ abstract class BaseDatabaseModel extends BaseModel implements
CurrentUserInterface,
CacheControllerFactoryAwareInterface
{
use DatabaseAwareTrait;
use DatabaseAwareTrait {
setDatabase as private setDatabaseTrait;
}
use MVCFactoryAwareTrait;
use DispatcherAwareTrait;
use CurrentUserTrait;
Expand All @@ -70,6 +75,16 @@ abstract class BaseDatabaseModel extends BaseModel implements
*/
protected $event_clean_cache = null;

/**
* The database driver.
*
* @var DatabaseInterface
* @since __DEPLOY_VERSION__
*
* @deprecated 5.0 Use getDatabase() instead of directly accessing _db
*/
protected $_db;

/**
* Constructor
*
Expand Down Expand Up @@ -353,6 +368,8 @@ protected function dispatchEvent(EventInterface $event)
*/
public function getDbo()
{
@trigger_error('Method getDbo() is deprecated, use getDatabase() instead.', E_USER_DEPRECATED);

try {
return $this->getDatabase();
} catch (DatabaseNotFoundException $e) {
Expand All @@ -373,6 +390,8 @@ public function getDbo()
*/
public function setDbo(DatabaseInterface $db = null)
{
@trigger_error('Method setDbo() is deprecated, use setDatabase() instead.', E_USER_DEPRECATED);

if ($db === null) {
return;
}
Expand All @@ -381,27 +400,19 @@ public function setDbo(DatabaseInterface $db = null)
}

/**
* Proxy for _db variable.
* Set the database.
* @todo: This is a workaround for backward compatibility, when $this->_db still in use. Should be removed in 5.0
*
* @param string $name The name of the element
* @param DatabaseInterface $db The database.
*
* @return mixed The value of the element if set, null otherwise
*
* @since 4.2.0
* @return void
*
* @deprecated 5.0 Use getDatabase() instead of directly accessing _db
* @since __DEPLOY_VERSION__
*/
public function __get($name)
public function setDatabase(DatabaseInterface $db): void
{
if ($name === '_db') {
return $this->getDatabase();
}

// Default the variable
if (!isset($this->$name)) {
$this->$name = null;
}

return $this->$name;
// Workaround for backward compatibility
$this->_db = $db;
$this->setDatabaseTrait($db);
}
}