Micro-ORM/index.php

57 lines
1.3 KiB
PHP
Raw Normal View History

2021-02-26 21:00:00 +00:00
<?php
2021-02-26 21:11:56 +00:00
namespace Sieciech\Orm8;
2021-02-26 21:00:00 +00:00
interface ISqlBase {
public function Get(): string;
//public function Insert(): string;
//public function Update(): string;
//public function Delete(): string;
}
2021-02-26 21:24:44 +00:00
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);
return $stmt->fetchObject($output);
}
}
2021-02-26 21:08:57 +00:00
abstract class BaseModel {
2021-02-26 21:24:44 +00:00
protected static $sql;
2021-02-26 21:00:00 +00:00
public function __construct(ISqlBase $sql) {
2021-02-26 21:24:44 +00:00
self::$sql = $sql;
2021-02-26 21:00:00 +00:00
}
public static function Get(int $id) {
2021-02-26 21:24:44 +00:00
return ConnectionManager::Query(
self::$sql->Get(),
[],
get_called_class());
2021-02-26 21:00:00 +00:00
}
}
class JobSql implements ISqlBase {
public function Get(): string {
2021-02-26 21:24:44 +00:00
return "Select * from jobs where id = :id";
2021-02-26 21:00:00 +00:00
}
}
class Job extends BaseModel {
public function __construct() {
parent::__construct( new JobSql() );
}
}
2021-02-26 21:24:44 +00:00
$dns = 'pgsql:host=localhost;port=5432;dbname=dynamic_dev;user=test;password=pas123';
ConnectionManager::Connect($dns);
2021-02-26 21:11:56 +00:00
echo "
2021-02-26 21:00:00 +00:00
2021-02-26 21:11:56 +00:00
";
2021-02-26 21:00:00 +00:00
echo Job::Get(10);
2021-02-26 21:11:56 +00:00
echo "
";