Basic model of club created according to abstact model

This commit is contained in:
Christian Wolf 2019-05-17 14:56:29 +02:00
parent 03df8c8f1a
commit c459e1d332
2 changed files with 44 additions and 123 deletions

View File

@ -74,9 +74,13 @@ abstract class AbstractClubsModel
$obj = new $className();
$obj->loadData($row);
$obj->loadCustomData($row);
return $obj;
}
protected function loadCustomData($assoc)
{}
public function save()
{
if($this->id === 'new')
@ -146,16 +150,15 @@ abstract class AbstractClubsModel
$dbo->execute();
}
protected function prepareDelete(){}
protected function prepareDelete($dbo){}
public function delete()
{
if($this->id === 'new')
return;
$this->prepareDelete();
$dbo = Factory::getDbo();
$this->prepareDelete($dbo);
$q = $dbo->getQuery(true);
$q->delete($this->getTableName())

View File

@ -1,11 +1,10 @@
<?php
// No direct access.
use Joomla\CMS\Factory;
defined('_JEXEC') or die;
class ClubsClub
class ClubsClub extends AbstractClubsModel
{
protected $id;
protected $name;
@ -16,7 +15,7 @@ class ClubsClub
protected $iban;
protected $bic;
protected $charitable;
protected $presidentId;
protected $president;
/**
* @return string
@ -79,7 +78,7 @@ class ClubsClub
*/
public function getPresidentId()
{
return $this->presidentId;
return $this->president->getId();
}
/**
@ -87,7 +86,7 @@ class ClubsClub
*/
public function getPresident()
{
return ClubsUser::loadUser((int) $this->presidentId);
return $this->president;
}
/**
@ -151,7 +150,7 @@ class ClubsClub
*/
public function setPresidentId(int $presidentId)
{
$this->presidentId = $presidentId;
$this->president = ClubsUser::loadUser($presidentId);
}
/**
@ -159,15 +158,7 @@ class ClubsClub
*/
public function setPresident(ClubsUser $user)
{
$this->presidentId = $user->getId();
}
/**
* @return int
*/
public function getId()
{
return $this->id;
$this->president = $user;
}
/**
@ -186,65 +177,21 @@ class ClubsClub
$this->name = $name;
}
protected function loadData(array $data)
{
$mapping = array('id', 'name', 'address', 'city', 'homepage', 'mail', 'iban', 'bic', 'charitable', 'presidentId');
foreach($mapping as $m)
$this->$m = $data[$m];
public static function loadClubs()
{
return self::loadElements(self::tableName, self::className);
}
public static function loadClub($id)
{
return self::loadElement($id, self::tableName, self::className);
}
protected function __construct()
{}
public static function loadClubs()
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->select('*')
->from('#__club_clubs');
$dbo->setQuery($q);
$dbo->execute();
$list = $dbo->loadAssocList('id');
$ret = array();
foreach($list as $c)
{
$co = new ClubsClub();
$co->loadData($c);
$ret[] = $co;
}
return $ret;
}
/**
* @param int $id
* @throws Exception
* @return ClubsOffer
*/
public static function loadClub(int $id)
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->select('*')->from('#__club_clubs')->where('id=' . (int) $id);
$dbo->setQuery($q);
$dbo->execute();
$row = $dbo->loadAssoc();
if($row == null)
{
throw new Exception("No club found.");
// TODO
}
$club = new ClubsClub();
$club->loadData($row);
return $club;
}
public static function createClub()
{
$club = new ClubsClub();
@ -253,68 +200,39 @@ class ClubsClub
}
public function save()
protected function loadCustomData($assoc)
{
if($this->id === 'new')
$this->insertClub();
else
$this->updateClub();
parent::loadCustomData($assoc);
$this->president = ClubsUser::loadUser($assoc['president']);
}
private function insertClub()
protected function postQuoteFilter(&$values)
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$mapping = array('name', 'address', 'city', 'homepage', 'mail', 'iban', 'bic', 'charitable', 'presidentId');
$values = array();
foreach($mapping as $m)
$values[$m] = $q->q($this->$m);
$q->insert('#__club_clubs')
->columns(array('name'))
->values(join(',', $values))
;
$dbo->transactionStart();
$dbo->setQuery($q);
$dbo->execute();
$this->id = $dbo->insertid();
$dbo->transactionCommit();
parent::postQuoteFilter($values);
$values['president'] = $this->president->getId();
}
private function updateClub()
protected function prepareDelete($dbo)
{}
protected function getDataMappings()
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$mapping = array('name', 'address', 'city', 'homepage', 'mail', 'iban', 'bic', 'charitable', 'presidentId');
$values = array();
foreach($mapping as $m)
$values[$m] = $q->q($this->$m);
$q->update('#__club_clubs');
foreach($mapping as $m)
$q->set($q->qn($m) . '=' . $values[$m]);
$q->where("id=". (int) $this->id);
$dbo->setQuery($q);
$dbo->execute();
return array('neme', 'address', 'city', 'homepage', 'mail', 'iban', 'bic', 'charitable');
}
public function delete()
protected function getRequiredDataMappings()
{
if($this->id === 'new')
return;
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->delete('#__club_clubs')
->where('id=' . (int) $this->id);
$dbo->setQuery($q);
$dbo->execute();
return array('neme', 'address', 'city', 'mail', 'iban', 'bic', 'charitable');
}
private const tableName = '#__club_clubs';
private const className = 'ClubsClub';
protected function getTableName()
{
return self::tableName;
}
}