Started working on abstract db models from scratch

This commit is contained in:
Christian Wolf 2019-05-21 17:17:29 +02:00
parent 852f110967
commit 51c910e51f
5 changed files with 153 additions and 0 deletions

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<buildpath> <buildpath>
<buildpathentry kind="src" path="src"/> <buildpathentry kind="src" path="src"/>
<buildpathentry external="true" kind="lib" path="/home/private/slt/joomla"/>
<buildpathentry external="true" kind="lib" path="/srv/slt-dev"/> <buildpathentry external="true" kind="lib" path="/srv/slt-dev"/>
<buildpathentry kind="con" path="org.eclipse.php.core.LANGUAGE"/> <buildpathentry kind="con" path="org.eclipse.php.core.LANGUAGE"/>
</buildpath> </buildpath>

View File

@ -0,0 +1,2 @@
eclipse.preferences.version=1
org.eclipse.php.ui.text.custom_code_templates=<?xml version\="1.0" encoding\="UTF-8" standalone\="no"?><templates/>

View File

@ -8,6 +8,8 @@ defined('_JEXEC') or die;
JLoader::discover('Clubs', JPATH_ROOT . '/administrator/components/com_clubs/mymodels'); JLoader::discover('Clubs', JPATH_ROOT . '/administrator/components/com_clubs/mymodels');
JLoader::registerPrefix('AbstractClubs', JPATH_ROOT . '/administrator/components/com_clubs/abstract'); JLoader::registerPrefix('AbstractClubs', JPATH_ROOT . '/administrator/components/com_clubs/abstract');
JLoader::registerPrefix('AbstractCommonClubs', JPATH_ROOT . '/administrator/components/com_clubs/common/abstract');
// JLoader::registerPrefix('ClubsHelper', JPATH_ROOT . '/administrator/components/com_clubs/common/helper');
$controller = BaseController::getInstance("Clubs"); $controller = BaseController::getInstance("Clubs");
$input = Factory::getApplication()->input; $input = Factory::getApplication()->input;

View File

@ -0,0 +1,37 @@
<?php
// No direct access.
defined('_JEXEC') or die;
abstract class AbstractCommonClubsModel
{
protected $id; // TODO private possible?
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
}
private $values = null;
protected function getValues()
{
if(is_null($this->values))
// FIXME fetch cache
return;
return $this->values;
}
public function setValues($values)
{
$this->values = $values;
}
}

View File

@ -0,0 +1,111 @@
<?php
// No direct access.
use Joomla\CMS\Factory;
defined('_JEXEC') or die;
class ElementNotFoundException extends Exception
{}
abstract class AbstractClubsModelFactory
{
/*
* This method should return an array to configure the trivially accessible values.
* The key must be a unique key describing the information and will be reused in the getValues() method.
* The value of each key should be an associative array describing the object field.
* Example of an array returned by this method is
* array(
* 'name'=>array('col'=>'Name', type=>'string'),
* 'size'=>array('col'=>'area', type=>'int')
* )
*
* The inner arrays contain the following entries:
* - col: The name of the column in the database. Mandatory.
* - type: The type of the column. If nothing is specified, string is assumed.
* - string
* - int
* - float
* - ref
* - ref: (only with type='ref') The name of the class that is referenced
*/
protected abstract function getAttributes();
private $attributes = null;
private function fetchAttributes()
{
if($this->attributes === null)
$this->attributes = $this->getAttributes();
return $this->attributes;
}
protected abstract function getTableName();
protected abstract function getClassName();
/**
*
* @param string $condition
* @return array
*/
protected function loadElements($condition = null)
{
$db = Factory::getDbo();
$q = $db->getQuery(true);
// $columns = array_map(function($arr) use ($q){ return $q->qn('main' . $arr['col']); }, $this->fetchAttributes());
$columns = array();
foreach($this->fetchAttributes() as $k=>$v)
{
$columns[] = $q->qn('main' . $v['col'], $k);
}
$q->select('main.id')->select($columns);
$q->from($this->getTableName(), 'main');
// TODO Joins
if($condition !== null)
$q->where($condition);
$db->setQuery($q);
$db->execute();
$it = $db->getIterator();
$ret = array();
while($it->valid())
{
$row = $it->current();
$ret[] = $this->generateObject($row);
$it->next();
}
return $ret;
}
/**
*
* @param int $id
*/
protected function loadById($id) {
$arr = $this->loadElements("main.id = " . ((int)$id) );
if(sizeof($arr) == 0)
throw new ElementNotFoundException();
return $arr[0];
}
protected function generateObject($row)
{
$name = $this->getClassName();
$obj = new $name();
$obj->setId($row['id']);
unset($row['id']);
$obj->setValues($row);
return $obj;
}
}