Started creating abstract model to simplify writing code

This commit is contained in:
Christian Wolf 2019-04-17 16:41:46 +02:00
parent 893dc754f0
commit 4b2dbff104
2 changed files with 37 additions and 114 deletions

View File

@ -96,7 +96,14 @@ abstract class ClubsAbstractModel
$mappings = $this->getDataMappings(); $mappings = $this->getDataMappings();
$values = array(); $values = array();
foreach($mappings as $m) foreach($mappings as $m)
$values[$m] = $q->q($this->$m); $values[$m] = $this->$m;
$this->preFilter($values);
foreach($mappings as $m)
$values[$m] = $q->q($values[$m]);
$this->postFilter($values);
$q->insert($this->getTableName()) $q->insert($this->getTableName())
->columns(array_map(array($q, 'qn'), $mappings)) ->columns(array_map(array($q, 'qn'), $mappings))
@ -121,7 +128,14 @@ abstract class ClubsAbstractModel
$mapping = $this->getDataMappings(); $mapping = $this->getDataMappings();
$values = array(); $values = array();
foreach($mapping as $m) foreach($mapping as $m)
$values[$m] = $q->q($this->$m); $values[$m] = $this->$m;
$this->preFilter($values);
foreach($mapping as $m)
$values[$m] = $q->q($values[$m]);
$this->postFilter($values);
$q->update($this->getTableName()); $q->update($this->getTableName());
foreach($mapping as $m) foreach($mapping as $m)
@ -152,4 +166,8 @@ abstract class ClubsAbstractModel
return true; return true;
} }
protected function preFilter(&$values){}
protected function postFilter(&$values){}
} }

View File

@ -1,23 +1,14 @@
<?php <?php
// No direct access. // No direct access.
use Joomla\CMS\Factory;
defined('_JEXEC') or die; defined('_JEXEC') or die;
class ClubsPlace class ClubsPlace extends ClubsAbstractModel
{ {
protected $id;
protected $name; protected $name;
protected $address; protected $address;
protected $area; protected $area;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/** /**
* @return string * @return string
@ -67,125 +58,39 @@ class ClubsPlace
$this->area = $area; $this->area = $area;
} }
protected function loadData(array $data)
{
$this->id = $data['id'];
$this->name = $data['name'];
}
protected function __construct() protected function __construct()
{} {}
public static function loadOffers() public static function loadPlaces()
{ {
$dbo = Factory::getDbo(); return self::loadElements(self::tableName, self::className);
$q = $dbo->getQuery(true);
$q->select('*')
->from('#__club_offers');
$dbo->setQuery($q);
$dbo->execute();
$list = $dbo->loadAssocList('id');
$ret = array();
foreach($list as $o)
{
$oo = new ClubsOffer();
$oo->loadData($o);
$ret[] = $oo;
}
return $ret;
} }
public static function loadOffer(int $id) public static function loadPlace(int $id)
{ {
$dbo = Factory::getDbo(); return self::loadElement($id, self::tableName, self::className);
$q = $dbo->getQuery(true);
$q->select('*')->from('#__club_offers')->where('id=' . (int) $id);
$dbo->setQuery($q);
$dbo->execute();
$row = $dbo->loadAssoc();
if($row == null)
{
throw new Exception("No offer found.");
// TODO
}
$offer = new ClubsOffer();
$offer->loadData($row);
return $offer;
} }
public static function createOffer() public static function createPlace()
{ {
$offer = new ClubsOffer(); $place = new ClubsPlace();
$offer->id = 'new'; $place->id = 'new';
return $offer; return $place;
} }
protected function getDataMappings()
public function save()
{ {
if($this->id === 'new') return array('name', 'address', 'area');
$this->insertOffer();
else
$this->updateOffer();
} }
private const tableName = '#__club_places';
private const className = 'ClubsPlace';
private function insertOffer() protected function getTableName()
{ {
$dbo = Factory::getDbo(); return self::tableName;
$q = $dbo->getQuery(true);
$vname = $q->q($this->name);
$q->insert('#__club_offers')
->columns(array('name'))
->values("$vname")
;
$dbo->transactionStart();
$dbo->setQuery($q);
$dbo->execute();
$this->id = $dbo->insertid();
$dbo->transactionCommit();
} }
private function updateOffer()
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$vname = $q->q($this->name);
$q->update('#__club_offers')
->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_offers')
->where('id=' . (int) $this->id);
$dbo->setQuery($q);
$dbo->execute();
}
} }