This repository has been archived on 2025-02-26. You can view files and clone it, but cannot push or open issues or pull requests.
szurubooru/src/Models/Entities/AbstractEntity.php
Marcin Kurczewski 50e4b40721 Upgraded to newest chibi
- Separate non-static router class
- Moved some setup code to new method, Core::init
- Persistent database connection between tests
2014-05-23 23:34:50 +02:00

66 lines
1.2 KiB
PHP

<?php
abstract class AbstractEntity implements IValidatable
{
protected $model;
protected $id;
protected $__cache = [];
public abstract function fillNew();
public abstract function fillFromDatabase($row);
public function __construct($model)
{
$this->model = $model;
}
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
}
public function getCache($key)
{
return isset($this->__cache[$key])
? $this->__cache[$key]
: null;
}
public function setCache($key, $value)
{
$this->__cache[$key] = $value;
}
public function removeCache($key)
{
unset($this->__cache[$key]);
}
public function resetCache()
{
$this->__cache = [];
}
public function hasCache($key)
{
return isset($this->__cache[$key]);
}
protected function getColumnWithCache($columnName)
{
if ($this->hasCache($columnName))
return $this->getCache($columnName);
$stmt = \Chibi\Sql\Statements::select();
$stmt->setTable($this->model->getTableName());
$stmt->setColumn($columnName);
$stmt->setCriterion(\Chibi\Sql\Functors::equals('id', new \Chibi\Sql\Binding($this->getId())));
$value = Core::getDatabase()->fetchOne($stmt)[$columnName];
$this->setCache($columnName, $value);
return $value;
}
}