parent
This commit is contained in:
parent
7542ae1ac9
commit
26679ace12
33
index.php
33
index.php
|
@ -18,14 +18,18 @@ class ConnectionManager {
|
||||||
public static function Query(string $query, $params, $output = 'stdObject') {
|
public static function Query(string $query, $params, $output = 'stdObject') {
|
||||||
$stmt = self::$connection->prepare($query);
|
$stmt = self::$connection->prepare($query);
|
||||||
$stmt->execute($params);
|
$stmt->execute($params);
|
||||||
|
$stmt->setFetchMode(PDO::FETCH_CLASS, $output);
|
||||||
$stmt->setFetchMode(PDO::FETCH_INTO, new $output());
|
|
||||||
|
|
||||||
return $stmt->fetch();
|
return $stmt->fetch();
|
||||||
}
|
}
|
||||||
|
public static function QueryAll(string $query, $params, $output = 'stdObject') {
|
||||||
|
$stmt = self::$connection->prepare($query);
|
||||||
|
$stmt->execute($params);
|
||||||
|
$stmt->setFetchMode(PDO::FETCH_CLASS, $output);
|
||||||
|
return $stmt->fetchAll();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
abstract class BaseModel {
|
abstract class BaseModel {
|
||||||
protected static $sql = null;
|
protected static ISqlBase $sql = null;
|
||||||
|
|
||||||
public static function Get(int $id) {
|
public static function Get(int $id) {
|
||||||
static::Initialize();
|
static::Initialize();
|
||||||
|
@ -35,16 +39,30 @@ abstract class BaseModel {
|
||||||
get_called_class());
|
get_called_class());
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract public static function Initialize();
|
protected static function QueryAll($sql, $params) {
|
||||||
|
static::Initialize();
|
||||||
|
return ConnectionManager::QueryAll(
|
||||||
|
$sql,
|
||||||
|
$params,
|
||||||
|
get_called_class());
|
||||||
}
|
}
|
||||||
|
|
||||||
class JobSql implements ISqlBase {
|
abstract public static function Initialize();
|
||||||
|
}
|
||||||
|
interface IJobSql implements ISqlBase {
|
||||||
|
public function GetLast10(): string;
|
||||||
|
}
|
||||||
|
class JobSql implements IJobSql {
|
||||||
public function Get(): string {
|
public function Get(): string {
|
||||||
return "Select * from jobs where id = :id";
|
return "Select * from jobs where id = :id";
|
||||||
}
|
}
|
||||||
|
public function GetLast10() {
|
||||||
|
return "select * from jobs order by id desc limit 10";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Job extends BaseModel {
|
class Job extends BaseModel {
|
||||||
|
protected static IJobSql $sql;
|
||||||
public int $id;
|
public int $id;
|
||||||
public float $budget;
|
public float $budget;
|
||||||
public static function Initialize() {
|
public static function Initialize() {
|
||||||
|
@ -52,6 +70,9 @@ class Job extends BaseModel {
|
||||||
self::$sql = new JobSql();
|
self::$sql = new JobSql();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public static function GetLast10() {
|
||||||
|
return self::QueryAll(self::$sql->GetLast10(), []);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$dns = 'pgsql:host=localhost;port=5432;dbname=dynamic_dev;user=test;password=pass123';
|
$dns = 'pgsql:host=localhost;port=5432;dbname=dynamic_dev;user=test;password=pass123';
|
||||||
ConnectionManager::Connect($dns);
|
ConnectionManager::Connect($dns);
|
||||||
|
|
Loading…
Reference in New Issue