loaing of model via arbitrary field and nice home page
All checks were successful
Build and push / Pulling repo on server (push) Successful in 3s

This commit is contained in:
2023-10-29 17:37:28 +01:00
parent fea847afd2
commit 4516486b8e
5 changed files with 120 additions and 7 deletions

View File

@ -9,7 +9,7 @@ class Model {
{
//redis
if ($GLOBALS['redis'])
$this->redis = $GLOBALS['redis'];
$GLOBALS['redis'] = $GLOBALS['redis'];
}
/**
@ -55,6 +55,13 @@ class Model {
return $data;
}
public function getField($field,$id=false)
{
if(!$id)
$id = $this->id;
return $GLOBALS['redis']->hget($this->dbTable.':'.$id,$field);
}
public function save()
{
$newgen = false;
@ -84,9 +91,31 @@ class Model {
return $this->id;
}
public function load($id)
/**
* @param $value The value to search for
* @param string $field The field to search in. HAS to be marked as unique otherwise will throw error
*/
public function load($value,$field='id')
{
$this->id = $id;
if($field!='id')
{
//sanity check. Check if $field is marked as unique
if(!in_array('unique',$this->dbFields[$field]))
throw new Exception($field.' is not unique');
//we need to find the id first
$keys = $GLOBALS['redis']->keys($this->dbTable.':*');
foreach($keys as $key){
$id = end(explode(':',$key));
$thisval = $GLOBALS['redis']->hget($this->dbTable.':'.$id,$field);
if($thisval==$value)
{
$this->id = $id;
break;
}
}
}
else
$this->id = $value;
if(!$GLOBALS['redis']->exists($this->dbTable.':'.$this->id))
throw new Exception($this->dbTable.':'.$this->id.' not found');
$keys = array_keys($this->dbFields);
@ -95,7 +124,7 @@ class Model {
{
$value = $GLOBALS['redis']->hget($this->dbTable.':'.$this->id,$key);
if($value!==NULL) //we'll leave null values
if($value!==NULL) //we'll leave null values alone
switch($this->dbFields[$key]['type'])
{
case 'int': $value = intval($value);break;