Started creating abstract model to simplify writing code
This commit is contained in:
parent
893dc754f0
commit
4b2dbff104
@ -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){}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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');
|
||||
return self::loadElements(self::tableName, self::className);
|
||||
}
|
||||
|
||||
$ret = array();
|
||||
foreach($list as $o)
|
||||
|
||||
public static function loadPlace(int $id)
|
||||
{
|
||||
$oo = new ClubsOffer();
|
||||
$oo->loadData($o);
|
||||
|
||||
$ret[] = $oo;
|
||||
return self::loadElement($id, self::tableName, self::className);
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
|
||||
public static function loadOffer(int $id)
|
||||
public static function createPlace()
|
||||
{
|
||||
$dbo = Factory::getDbo();
|
||||
$q = $dbo->getQuery(true);
|
||||
$q->select('*')->from('#__club_offers')->where('id=' . (int) $id);
|
||||
$dbo->setQuery($q);
|
||||
$dbo->execute();
|
||||
$place = new ClubsPlace();
|
||||
$place->id = 'new';
|
||||
return $place;
|
||||
}
|
||||
|
||||
$row = $dbo->loadAssoc();
|
||||
|
||||
if($row == null)
|
||||
protected function getDataMappings()
|
||||
{
|
||||
throw new Exception("No offer found.");
|
||||
// TODO
|
||||
return array('name', 'address', 'area');
|
||||
}
|
||||
|
||||
$offer = new ClubsOffer();
|
||||
$offer->loadData($row);
|
||||
return $offer;
|
||||
}
|
||||
private const tableName = '#__club_places';
|
||||
private const className = 'ClubsPlace';
|
||||
|
||||
public static function createOffer()
|
||||
protected function getTableName()
|
||||
{
|
||||
$offer = new ClubsOffer();
|
||||
$offer->id = 'new';
|
||||
return $offer;
|
||||
}
|
||||
|
||||
|
||||
public function save()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user