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

View File

@ -1,23 +1,14 @@
<?php
// No direct access.
use Joomla\CMS\Factory;
defined('_JEXEC') or die;
class ClubsPlace
class ClubsPlace extends ClubsAbstractModel
{
protected $id;
protected $name;
protected $address;
protected $area;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return string
@ -67,124 +58,38 @@ class ClubsPlace
$this->area = $area;
}
protected function loadData(array $data)
{
$this->id = $data['id'];
$this->name = $data['name'];
}
protected function __construct()
{}
public static function loadOffers()
public static function loadPlaces()
{
$dbo = Factory::getDbo();
$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;
return self::loadElements(self::tableName, self::className);
}
public static function loadOffer(int $id)
public static function loadPlace(int $id)
{
$dbo = Factory::getDbo();
$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
return self::loadElement($id, self::tableName, self::className);
}
$offer = new ClubsOffer();
$offer->loadData($row);
return $offer;
public static function createPlace()
{
$place = new ClubsPlace();
$place->id = 'new';
return $place;
}
public static function createOffer()
protected function getDataMappings()
{
$offer = new ClubsOffer();
$offer->id = 'new';
return $offer;
return array('name', 'address', 'area');
}
private const tableName = '#__club_places';
private const className = 'ClubsPlace';
public function save()
protected function getTableName()
{
if($this->id === 'new')
$this->insertOffer();
else
$this->updateOffer();
}
private function insertOffer()
{
$dbo = Factory::getDbo();
$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();
return self::tableName;
}