Created abstract structures to keep complexity and code redundancy at bay.

This commit is contained in:
2019-04-18 14:31:13 +02:00
parent 4b2dbff104
commit 6e1326240b
10 changed files with 339 additions and 351 deletions

View File

@@ -1,21 +1,11 @@
<?php
// No direct access.
use Joomla\CMS\Factory;
defined('_JEXEC') or die;
class ClubsPosition
class ClubsPosition extends ClubsAbstractModel
{
protected $id;
protected $name;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return string
@@ -33,57 +23,21 @@ class ClubsPosition
$this->name = $name;
}
protected function loadData(array $data)
{
$this->id = $data['id'];
$this->name = $data['name'];
}
protected function __construct()
{}
private const tableName = '#__club_positions';
private const className = 'ClubsPosition';
public static function loadPositions()
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->select('*')
->from('#__club_positions');
$dbo->setQuery($q);
$dbo->execute();
$list = $dbo->loadAssocList('id');
$ret = array();
foreach($list as $p)
{
$po = new ClubsPosition();
$po->loadData($p);
$ret[] = $po;
}
return $ret;
return self::loadElements(self::tableName, self::className);
}
public static function loadPosition(int $id)
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->select('*')->from('#__club_positions')->where('id=' . (int) $id);
$dbo->setQuery($q);
$dbo->execute();
$row = $dbo->loadAssoc();
if($row == null)
{
throw new Exception("No position found.");
// TODO
}
$position = new ClubsPosition();
$position->loadData($row);
return $position;
return self::loadElement($id, self::tableName, self::className);
}
public static function createPosition()
@@ -93,65 +47,16 @@ class ClubsPosition
return $position;
}
public function save()
protected function getDataMappings()
{
if($this->id === 'new')
$this->insertPosition();
else
$this->updatePosition();
return array('name');
}
private function insertPosition()
protected function getTableName()
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$vname = $q->q($this->name);
$q->insert('#__club_positions')
->columns(array('name'))
->values("$vname")
;
$dbo->transactionStart();
$dbo->setQuery($q);
$dbo->execute();
$this->id = $dbo->insertid();
$dbo->transactionCommit();
}
private function updatePosition()
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$vname = $q->q($this->name);
$q->update('#__club_positions')
->set(array(
"name=$vname"
))
->where("id=". (int) $this->id)
;
$dbo->setQuery($q);
$dbo->execute();
}
public function delete()
{
if($this->id === 'new')
return;
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->delete('#__club_positions')
->where('id=' . (int) $this->id);
$dbo->setQuery($q);
$dbo->execute();
return self::tableName;
}
}