This commit is contained in:
Michal Sieciechowicz 2021-09-29 17:19:48 +02:00
parent 83f328d8ff
commit 73af54b952
1 changed files with 14 additions and 9 deletions

View File

@ -7,7 +7,7 @@ use Fufle\ORM\Database\ConnectionManager;
abstract class Model { abstract class Model {
protected static string $primaryKey = 'id'; protected static string $primaryKey = 'id';
protected static ?string $table = null; protected static ?string $table = null;
private static ?IQuery $query = null; protected static ?IQuery $query = null;
protected static array $fields = []; protected static array $fields = [];
@ -15,7 +15,7 @@ abstract class Model {
$array = []; $array = [];
$fields = static::$fields; $fields = static::$fields;
foreach($fields as $field) { foreach($fields as $field) {
if ($this->{$field} ?? null !== null) { if (isset($this->{$field})) {
$array[$field] = $this->{$field}; $array[$field] = $this->{$field};
} }
} }
@ -23,10 +23,7 @@ abstract class Model {
} }
protected static function QueryModel() { protected static function QueryModel() {
if (true || (self::$query === null && self::class !== static::class)) { return static::SetQueryModel();
self::$query = static::SetQueryModel();
}
return self::$query;
} }
public static function GetTableName() { public static function GetTableName() {
@ -47,18 +44,26 @@ abstract class Model {
} }
protected static function Query(string $sql, array|object $params = []) { protected static function Query(string $sql, array|object $params = []) {
return ConnectionManager::Query( $result = ConnectionManager::Query(
$sql, $sql,
$params, $params,
get_called_class()); get_called_class());
if (!$result) {
return null;
}
return $result;
} }
protected static function QueryAll(string $sql, array|object $params = []) { protected static function QueryAll(string $sql, array|object $params = []) {
return ConnectionManager::QueryAll( $result = ConnectionManager::QueryAll(
$sql, $sql,
$params, $params,
get_called_class()); get_called_class());
if (!$result) {
return [];
}
return $result;
} }
abstract public static function SetQueryModel(): IQuery; abstract public static function SetQueryModel(): IQuery;
} }