Skip to content

Commit

Permalink
[4.2] Change the db calls back to the getDbo (#38506)
Browse files Browse the repository at this point in the history
* Change the db calls back to the getDbo

* add test
  • Loading branch information
laoneo committed Aug 18, 2022
1 parent 196c9e5 commit 1ce74dc
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 15 deletions.
6 changes: 3 additions & 3 deletions libraries/src/MVC/Model/AdminModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ protected function batchCopy($value, $pks, $contexts)
}

$newIds = array();
$db = $this->getDatabase();
$db = $this->getDbo();

// Parent exists so let's proceed
while (!empty($pks)) {
Expand Down Expand Up @@ -840,7 +840,7 @@ public function delete(&$pks)

// Multilanguage: if associated, delete the item in the _associations table
if ($this->associationsContext && Associations::isEnabled()) {
$db = $this->getDatabase();
$db = $this->getDbo();
$query = $db->getQuery(true)
->select(
[
Expand Down Expand Up @@ -1305,7 +1305,7 @@ public function save($data)
}

// Get associationskey for edited item
$db = $this->getDatabase();
$db = $this->getDbo();
$id = (int) $table->$key;
$query = $db->getQuery(true)
->select($db->quoteName('key'))
Expand Down
23 changes: 12 additions & 11 deletions libraries/src/MVC/Model/BaseDatabaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ abstract class BaseDatabaseModel extends BaseModel implements
DatabaseModelInterface,
DispatcherAwareInterface,
CurrentUserInterface,
CacheControllerFactoryAwareInterface
CacheControllerFactoryAwareInterface,
DatabaseAwareInterface
{
use DatabaseAwareTrait;
use MVCFactoryAwareTrait;
Expand Down Expand Up @@ -151,13 +152,13 @@ public function __construct($config = array(), MVCFactoryInterface $factory = nu
protected function _getList($query, $limitstart = 0, $limit = 0)
{
if (\is_string($query)) {
$query = $this->getDatabase()->getQuery(true)->setQuery($query);
$query = $this->getDbo()->getQuery(true)->setQuery($query);
}

$query->setLimit($limit, $limitstart);
$this->getDatabase()->setQuery($query);
$this->getDbo()->setQuery($query);

return $this->getDatabase()->loadObjectList();
return $this->getDbo()->loadObjectList();
}

/**
Expand Down Expand Up @@ -188,9 +189,9 @@ protected function _getListCount($query)
$query = clone $query;
$query->clear('select')->clear('order')->clear('limit')->clear('offset')->select('COUNT(*)');

$this->getDatabase()->setQuery($query);
$this->getDbo()->setQuery($query);

return (int) $this->getDatabase()->loadResult();
return (int) $this->getDbo()->loadResult();
}

// Otherwise fall back to inefficient way of counting all results.
Expand All @@ -201,10 +202,10 @@ protected function _getListCount($query)
$query->clear('limit')->clear('offset')->clear('order');
}

$this->getDatabase()->setQuery($query);
$this->getDatabase()->execute();
$this->getDbo()->setQuery($query);
$this->getDbo()->execute();

return (int) $this->getDatabase()->getNumRows();
return (int) $this->getDbo()->getNumRows();
}

/**
Expand All @@ -223,7 +224,7 @@ protected function _createTable($name, $prefix = 'Table', $config = array())
{
// Make sure we are returning a DBO object
if (!\array_key_exists('dbo', $config)) {
$config['dbo'] = $this->getDatabase();
$config['dbo'] = $this->getDbo();
}

return $this->getMVCFactory()->createTable($name, $prefix, $config);
Expand Down Expand Up @@ -394,7 +395,7 @@ public function setDbo(DatabaseInterface $db = null)
public function __get($name)
{
if ($name === '_db') {
return $this->getDatabase();
return $this->getDbo();
}

// Default the variable
Expand Down
2 changes: 1 addition & 1 deletion libraries/src/MVC/Model/ListModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public function getItems()
*/
protected function getListQuery()
{
return $this->getDatabase()->getQuery(true);
return $this->getDbo()->getQuery(true);
}

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/Unit/Libraries/Cms/MVC/Model/DatabaseModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,48 @@ public function testCheckedOutWitFieldEmptyUserSet()
$this->assertTrue($model->isCheckedOut((object)['checked_out' => 1]));
}

/**
* @testdox that all function calls go over deprecated getDbo function
*
* @return void
*
* @since __DEPLOY_VERSION__
*
* @deprecated 5.0 Must be removed when database calls are changed to getDatabase in libraries models
*/
public function testOverrideOldDboFunction()
{
$db = $this->createMock(DatabaseInterface::class);
$db->expects($this->never())->method('setQuery');

$query = $this->getQueryStub($db);
$newDb = $this->createMock(DatabaseInterface::class);
$newDb->expects($this->once())->method('setQuery')->with($this->equalTo($query));

$model = new class (['dbo' => $db], $this->createStub(MVCFactoryInterface::class), $newDb) extends BaseDatabaseModel
{
private $newDb;

public function __construct(array $config, MVCFactoryInterface $factory, DatabaseInterface $newDb)
{
parent::__construct($config, $factory);

$this->newDb = $newDb;
}

public function _getList($query, $limitstart = 0, $limit = 0)
{
return parent::_getList($query, $limitstart, $limit);
}

public function getDbo()
{
return $this->newDb;
}
};
$model->_getList($query, 0, 1);
}

/**
* @testdox still can use the old trait
*
Expand Down

0 comments on commit 1ce74dc

Please sign in to comment.