Prepared work of models to be useful with basic attributes

This commit is contained in:
Christian Wolf 2019-05-22 14:03:18 +02:00
parent fd0a0f7f4d
commit cb6068bfd6
2 changed files with 123 additions and 17 deletions

View File

@ -8,8 +8,12 @@ defined('_JEXEC') or die;
abstract class AbstractCommonClubsModel abstract class AbstractCommonClubsModel
{ {
protected $id; // TODO private possible? // TODO Adddata validator
protected $new; // TODO Make setting of values attribute fail in case of problems
// FIXME Add Joins in select statements
private $id;
private $new;
public function getId() public function getId()
{ {
@ -98,7 +102,73 @@ abstract class AbstractCommonClubsModel
$db->setQuery($q); $db->setQuery($q);
$db->execute(); $db->execute();
$this->values = $db->loadAssoc(); $values = $db->loadAssoc();
$values = $this->unpackExternalReferencesFromKeys($values);
$this->values = $values;
}
private function packExternalReferencesAsKeys($vals)
{
foreach($this->getFactory()->getAttributes() as $k => $v)
{
if($v['type'] !== 'ref')
continue;
$vals[$k] = $vals[$k]->getId();
}
return $vals;
}
private function unpackExternalReferencesFromKeys($vals)
{
foreach($this->getFactory()->getAttributes() as $k => $v)
{
if($v['type'] !== 'ref')
continue;
if(empty($v['ref']))
throw new Exception('External reference of unknown class found.');
$vals[$k] = $this->loadExternalReferenceAsObject($v['ref'], $vals[$k]);
}
return $vals;
}
private function loadExternalReferenceAsObject($className, $value)
{
if(! is_int($value))
throw new Exception('Reference with non-integer value');
$factory = $this->getFactoryOfClass($className);
return $factory->loadById($value);
}
private static const CLASSNAME_MAP = array(
);
/**
* @todo This must be done better and more cleanly
* @param string $className
* @return AbstractCommonClubsModelFactory
*/
private function getFactoryOfClass($className)
{
if(empty(self::CLASSNAME_MAP[$className]))
{
$parts = array();
if(preg_match('/^CommonClubsModel(.*)/', $className, $parts))
{
return "CommonClubsModelFactory{$parts[1]}";
}
throw new Exception("Unknown mapping of class $className");
}
return self::CLASSNAME_MAP[$className];
} }
/** /**
@ -146,12 +216,12 @@ abstract class AbstractCommonClubsModel
return $quotedData; return $quotedData;
} }
protected function filterRawData($values) protected function filterDatabaseRawData($values)
{ {
return $values; return $values;
} }
protected function filterQuotedData($quoted) protected function filterDatabaseQuotedData($quoted)
{ {
return $quoted; return $quoted;
} }
@ -165,10 +235,10 @@ abstract class AbstractCommonClubsModel
private function getQuotedData($attribs, $q) private function getQuotedData($attribs, $q)
{ {
$rawData = $this->getValues(); $rawData = $this->getValues();
$rawData = $this->filterRawData($rawData); $rawData = $this->filterDatabaseRawData($rawData);
$quotedData = $this->quoteData($rawData, $attribs, $q); $quotedData = $this->quoteData($rawData, $attribs, $q);
$quotedData = $this->filterQuotedData($quotedData); $quotedData = $this->filterDatabaseQuotedData($quotedData);
return $quotedData; return $quotedData;
} }
@ -239,14 +309,47 @@ abstract class AbstractCommonClubsModel
$db->transactionCommit(); $db->transactionCommit();
} }
/**
*
* @param AbstractCommonClubsModelFactory $factory
* @param string $colName
*/
protected function fetchAssociatedElements($factory, $colName)
{
$condition = "main.$colName = {$this->id}";
return $factory->loadElements($condition);
}
protected function filterPackData($values)
{
return $values;
}
protected function filterUnpackData($values)
{
return $values;
}
public function pack() public function pack()
{ {
// TODO $vals = $this->getValues();
$vals = $this->packExternalReferencesAsKeys($vals);
$vals = $this->filterPackData($vals);
$json = json_encode($vals);
return urldecode($json);
} }
public function unpack($str) public function unpack($str)
{ {
// TODO $json = urlencode($str);
$data = json_decode($json, true);
$vals = $this->unpackExternalReferencesFromKeys($data);
$vals = $this->filterUnpackData($vals);
$this->setValues($vals);
} }
} }

View File

@ -55,13 +55,13 @@ abstract class AbstractCommonClubsModelFactory
$q = $db->getQuery(true); $q = $db->getQuery(true);
// $columns = array_map(function($arr) use ($q){ return $q->qn('main' . $arr['col']); }, $this->fetchAttributes()); // $columns = array_map(function($arr) use ($q){ return $q->qn('main' . $arr['col']); }, $this->fetchAttributes());
$columns = array(); // $columns = array();
foreach($this->fetchAttributes() as $k=>$v) // foreach($this->fetchAttributes() as $k=>$v)
{ // {
$columns[] = $q->qn('main' . $v['col'], $k); // $columns[] = $q->qn('main' . $v['col'], $k);
} // }
$q->select('main.id')->select($columns); $q->select('main.id AS id');//->select($columns);
$q->from($this->getTableName(), 'main'); $q->from($this->getTableName(), 'main');
// TODO Joins // TODO Joins
@ -111,8 +111,11 @@ abstract class AbstractCommonClubsModelFactory
$obj = $this->generatePlainObject($row['id']); $obj = $this->generatePlainObject($row['id']);
$obj->marksAsNew(false); $obj->marksAsNew(false);
unset($row['id']); //unset($row['id']);
$obj->setValues($row); //$obj->setValues($row);
// Do not trigger cache if no needed
//$obj->getValues();
return $obj; return $obj;
} }