Micro-ORM/index.php

64 lines
1.5 KiB
PHP

<?php
namespace Sieciech\Orm8;
use PDO;
interface ISqlBase {
public function Get(): string;
//public function Insert(): string;
//public function Update(): string;
//public function Delete(): string;
}
class ConnectionManager {
private static $connection;
public static function connect(string $connectionString) {
self::$connection = new PDO($connectionString);
}
public static function Query(string $query, $params, $output = 'stdObject') {
$stmt = self::$connection->prepare($query);
$stmt->execute($params);
$stmt->setFetchMode(PDO::FETCH_INTO, $output);
return $stmt->fetch();
}
}
abstract class BaseModel {
protected static $sql = null;
public static function Get(int $id) {
static::Initialize();
return ConnectionManager::Query(
self::$sql->Get(),
['id' => $id],
get_called_class());
}
abstract public static function Initialize();
}
class JobSql implements ISqlBase {
public function Get(): string {
return "Select * from jobs where id = :id";
}
}
class Job extends BaseModel {
public int $id;
public float $budget;
public static function Initialize() {
if (self::$sql === null) {
self::$sql = new JobSql();
}
}
}
$dns = 'pgsql:host=localhost;port=5432;dbname=dynamic_dev;user=test;password=pass123';
ConnectionManager::Connect($dns);
echo "
";
var_dump(Job::Get(10));
echo "
";