64 lines
1.7 KiB
PHP
64 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace Fufle\ORM;
|
|
|
|
use Fufle\ORM\Database\ConnectionManager;
|
|
|
|
abstract class Model {
|
|
protected static string $primaryKey = 'id';
|
|
protected static ?string $table = null;
|
|
private static ?IQuery $query = null;
|
|
|
|
protected static array $fields = [];
|
|
|
|
public function toArray(): array {
|
|
$array = [];
|
|
$fields = static::$fields;
|
|
foreach($fields as $field) {
|
|
if ($this->{$field} ?? null !== null) {
|
|
$array[$field] = $this->{$field};
|
|
}
|
|
}
|
|
return $array;
|
|
}
|
|
|
|
protected static function QueryModel() {
|
|
if (true || (self::$query === null && self::class !== static::class)) {
|
|
self::$query = static::SetQueryModel();
|
|
}
|
|
return self::$query;
|
|
}
|
|
|
|
public static function GetTableName() {
|
|
if (static::$table === null) {
|
|
static::$table = basename(str_replace('\\', '/', get_called_class())) . 's';
|
|
}
|
|
return static::$table;
|
|
}
|
|
|
|
public static function GetPrimaryKey() {
|
|
return static::$primaryKey;
|
|
}
|
|
|
|
public static function Get(int $id) {
|
|
return self::Query(
|
|
static::QueryModel()->Get(),
|
|
[$id]);
|
|
}
|
|
|
|
protected static function Query(string $sql, array|object $params = []) {
|
|
return ConnectionManager::Query(
|
|
$sql,
|
|
$params,
|
|
get_called_class());
|
|
}
|
|
|
|
protected static function QueryAll(string $sql, array|object $params = []) {
|
|
return ConnectionManager::QueryAll(
|
|
$sql,
|
|
$params,
|
|
get_called_class());
|
|
}
|
|
|
|
abstract public static function SetQueryModel(): IQuery;
|
|
} |