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

View File

@ -1,11 +1,10 @@
<?php <?php
// No direct access. // No direct access.
use Joomla\CMS\Factory;
defined('_JEXEC') or die; defined('_JEXEC') or die;
class ClubsClub class ClubsClub extends AbstractClubsModel
{ {
protected $id; protected $id;
protected $name; protected $name;
@ -16,7 +15,7 @@ class ClubsClub
protected $iban; protected $iban;
protected $bic; protected $bic;
protected $charitable; protected $charitable;
protected $presidentId; protected $president;
/** /**
* @return string * @return string
@ -79,7 +78,7 @@ class ClubsClub
*/ */
public function getPresidentId() public function getPresidentId()
{ {
return $this->presidentId; return $this->president->getId();
} }
/** /**
@ -87,7 +86,7 @@ class ClubsClub
*/ */
public function getPresident() public function getPresident()
{ {
return ClubsUser::loadUser((int) $this->presidentId); return $this->president;
} }
/** /**
@ -151,7 +150,7 @@ class ClubsClub
*/ */
public function setPresidentId(int $presidentId) public function setPresidentId(int $presidentId)
{ {
$this->presidentId = $presidentId; $this->president = ClubsUser::loadUser($presidentId);
} }
/** /**
@ -159,15 +158,7 @@ class ClubsClub
*/ */
public function setPresident(ClubsUser $user) public function setPresident(ClubsUser $user)
{ {
$this->presidentId = $user->getId(); $this->president = $user;
}
/**
* @return int
*/
public function getId()
{
return $this->id;
} }
/** /**
@ -185,136 +176,63 @@ class ClubsClub
{ {
$this->name = $name; $this->name = $name;
} }
protected function loadData(array $data)
public static function loadClubs()
{ {
$mapping = array('id', 'name', 'address', 'city', 'homepage', 'mail', 'iban', 'bic', 'charitable', 'presidentId'); return self::loadElements(self::tableName, self::className);
}
foreach($mapping as $m)
$this->$m = $data[$m]; public static function loadClub($id)
{
return self::loadElement($id, self::tableName, self::className);
} }
protected function __construct() 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() public static function createClub()
{ {
$club = new ClubsClub(); $club = new ClubsClub();
$club->id = 'new'; $club->id = 'new';
return $club; return $club;
} }
public function save() protected function loadCustomData($assoc)
{ {
if($this->id === 'new') parent::loadCustomData($assoc);
$this->insertClub(); $this->president = ClubsUser::loadUser($assoc['president']);
else
$this->updateClub();
} }
private function insertClub() protected function postQuoteFilter(&$values)
{ {
$dbo = Factory::getDbo(); parent::postQuoteFilter($values);
$q = $dbo->getQuery(true); $values['president'] = $this->president->getId();
$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();
} }
private function updateClub() protected function prepareDelete($dbo)
{}
protected function getDataMappings()
{ {
$dbo = Factory::getDbo(); return array('neme', 'address', 'city', 'homepage', 'mail', 'iban', 'bic', 'charitable');
$q = $dbo->getQuery(true); }
$mapping = array('name', 'address', 'city', 'homepage', 'mail', 'iban', 'bic', 'charitable', 'presidentId'); protected function getRequiredDataMappings()
$values = array(); {
foreach($mapping as $m) return array('neme', 'address', 'city', 'mail', 'iban', 'bic', 'charitable');
$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();
} }
public function delete() private const tableName = '#__club_clubs';
private const className = 'ClubsClub';
protected function getTableName()
{ {
if($this->id === 'new') return self::tableName;
return;
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->delete('#__club_clubs')
->where('id=' . (int) $this->id);
$dbo->setQuery($q);
$dbo->execute();
} }
} }