2022-06-17 20:53:35 +02:00
|
|
|
<?php
|
|
|
|
|
2022-11-20 17:13:53 +01:00
|
|
|
namespace Core\Objects\DatabaseEntity\Controller;
|
2022-06-17 20:53:35 +02:00
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
use Core\Driver\SQL\Condition\Compare;
|
|
|
|
use Core\Driver\SQL\Condition\Condition;
|
|
|
|
use Core\Driver\SQL\SQL;
|
2022-06-17 20:53:35 +02:00
|
|
|
|
|
|
|
abstract class DatabaseEntity {
|
|
|
|
|
2022-11-20 17:13:53 +01:00
|
|
|
protected static array $entityLogConfig = [
|
|
|
|
"insert" => false,
|
|
|
|
"update" => false,
|
|
|
|
"delete" => false,
|
|
|
|
"lifetime" => null,
|
|
|
|
];
|
|
|
|
|
2022-06-17 20:53:35 +02:00
|
|
|
private static array $handlers = [];
|
2022-06-20 19:52:31 +02:00
|
|
|
protected ?int $id;
|
2022-06-17 20:53:35 +02:00
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
public function __construct(?int $id = null) {
|
|
|
|
$this->id = $id;
|
2022-06-17 20:53:35 +02:00
|
|
|
}
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
public abstract function jsonSerialize(): array;
|
|
|
|
|
|
|
|
public function preInsert(array &$row) { }
|
|
|
|
public function postFetch(SQL $sql, array $row) { }
|
|
|
|
|
|
|
|
public static function fromRow(SQL $sql, array $row): static {
|
|
|
|
$handler = self::getHandler($sql);
|
|
|
|
return $handler->entityFromRow($row);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function newInstance(\ReflectionClass $reflectionClass, array $row) {
|
|
|
|
return $reflectionClass->newInstanceWithoutConstructor();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function find(SQL $sql, int $id, bool $fetchEntities = false, bool $fetchRecursive = false): static|bool|null {
|
2022-06-17 22:58:42 +02:00
|
|
|
$handler = self::getHandler($sql);
|
2022-06-20 19:52:31 +02:00
|
|
|
if ($fetchEntities) {
|
|
|
|
return DatabaseEntityQuery::fetchOne(self::getHandler($sql))
|
|
|
|
->where(new Compare($handler->getTableName() . ".id", $id))
|
|
|
|
->fetchEntities($fetchRecursive)
|
|
|
|
->execute();
|
|
|
|
} else {
|
|
|
|
return $handler->fetchOne($id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function exists(SQL $sql, int $id): bool {
|
|
|
|
$handler = self::getHandler($sql);
|
|
|
|
$res = $sql->select($sql->count())
|
|
|
|
->from($handler->getTableName())
|
|
|
|
->where(new Compare($handler->getTableName() . ".id", $id))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
return $res !== false && $res[0]["count"] !== 0;
|
|
|
|
}
|
|
|
|
|
2022-11-20 17:13:53 +01:00
|
|
|
public static function findBy(DatabaseEntityQuery $dbQuery): static|array|bool|null {
|
|
|
|
return $dbQuery->execute();
|
2022-06-17 20:53:35 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 23:29:24 +02:00
|
|
|
public static function findAll(SQL $sql, ?Condition $condition = null): ?array {
|
2022-06-17 22:58:42 +02:00
|
|
|
$handler = self::getHandler($sql);
|
|
|
|
return $handler->fetchMultiple($condition);
|
2022-06-17 20:53:35 +02:00
|
|
|
}
|
|
|
|
|
2022-11-20 17:13:53 +01:00
|
|
|
public static function createBuilder(SQL $sql, bool $one): DatabaseEntityQuery {
|
|
|
|
if ($one) {
|
|
|
|
return DatabaseEntityQuery::fetchOne(self::getHandler($sql));
|
|
|
|
} else {
|
|
|
|
return DatabaseEntityQuery::fetchAll(self::getHandler($sql));
|
|
|
|
}
|
2022-06-20 19:52:31 +02:00
|
|
|
}
|
|
|
|
|
2022-11-20 17:13:53 +01:00
|
|
|
public function save(SQL $sql, ?array $columns = null, bool $saveNM = false): bool {
|
2022-06-17 22:58:42 +02:00
|
|
|
$handler = self::getHandler($sql);
|
2022-11-20 17:13:53 +01:00
|
|
|
$res = $handler->insertOrUpdate($this, $columns, $saveNM);
|
2022-06-17 20:53:35 +02:00
|
|
|
if ($res === false) {
|
|
|
|
return false;
|
|
|
|
} else if ($this->id === null) {
|
2022-08-20 22:17:17 +02:00
|
|
|
$this->id = $res;
|
2022-11-20 17:13:53 +01:00
|
|
|
$handler->insertNM($this);
|
2022-08-20 22:17:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function insert(SQL $sql): bool {
|
|
|
|
$handler = self::getHandler($sql);
|
|
|
|
$res = $handler->insert($this);
|
|
|
|
if ($res === false) {
|
|
|
|
return false;
|
|
|
|
} else if ($this->id === null) {
|
2022-06-17 20:53:35 +02:00
|
|
|
$this->id = $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete(SQL $sql): bool {
|
2022-06-17 22:58:42 +02:00
|
|
|
$handler = self::getHandler($sql);
|
2022-06-17 20:53:35 +02:00
|
|
|
if ($this->id === null) {
|
2022-06-17 22:58:42 +02:00
|
|
|
$handler->getLogger()->error("Cannot delete entity without id");
|
2022-06-17 20:53:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-17 23:29:24 +02:00
|
|
|
if ($handler->delete($this->id)) {
|
|
|
|
$this->id = null;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2022-06-17 20:53:35 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 22:58:42 +02:00
|
|
|
public static function getHandler(SQL $sql, $obj_or_class = null): DatabaseEntityHandler {
|
2022-06-17 20:53:35 +02:00
|
|
|
|
|
|
|
if (!$obj_or_class) {
|
|
|
|
$obj_or_class = get_called_class();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!($obj_or_class instanceof \ReflectionClass)) {
|
|
|
|
$class = new \ReflectionClass($obj_or_class);
|
|
|
|
} else {
|
|
|
|
$class = $obj_or_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
$handler = self::$handlers[$class->getShortName()] ?? null;
|
|
|
|
if (!$handler) {
|
2022-06-17 22:58:42 +02:00
|
|
|
$handler = new DatabaseEntityHandler($sql, $class);
|
2022-06-17 20:53:35 +02:00
|
|
|
self::$handlers[$class->getShortName()] = $handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getId(): ?int {
|
|
|
|
return $this->id;
|
|
|
|
}
|
2022-11-20 17:13:53 +01:00
|
|
|
|
|
|
|
public static function count(SQL $sql, ?Condition $condition = null): int|bool {
|
|
|
|
$handler = self::getHandler($sql);
|
|
|
|
$query = $sql->select($sql->count())
|
|
|
|
->from($handler->getTableName());
|
|
|
|
|
|
|
|
if ($condition) {
|
|
|
|
$query->where($condition);
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = $query->execute();
|
|
|
|
|
|
|
|
if (!empty($res)) {
|
|
|
|
return $res[0]["count"];
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2022-06-17 20:53:35 +02:00
|
|
|
}
|