package
This commit is contained in:
parent
64c3c3f44b
commit
c91ccb9480
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
use App\Query\IJobQuery;
|
||||
use App\Query\JobQuery;
|
||||
use Fufle\ORM\Model as BaseModel;
|
||||
|
||||
class JobModel extends BaseModel {
|
||||
//private string $table = 'jobs';
|
||||
protected static IJobQuery $query;
|
||||
public int $id;
|
||||
public float $budget;
|
||||
public static function Initialize() {
|
||||
if (self::$query === null) {
|
||||
self::$query = new JobQuery($this);
|
||||
}
|
||||
}
|
||||
public static function GetLast10() {
|
||||
return self::QueryAll(self::$query->GetLast10());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace App\Query;
|
||||
|
||||
use Fufle\ORM\IQuery as IBaseQuery;
|
||||
|
||||
interface IJobQuery extends IBaseQuery {
|
||||
public function GetLast10(): string;
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace App\Query;
|
||||
|
||||
use App\Query\IJobQuery;
|
||||
use Fufle\ORM\Query as BaseQuery;
|
||||
|
||||
class JobQuery extends BaseQuery implements IJobQuery {
|
||||
public function GetLast10() {
|
||||
return "select * from jobs order by id desc limit 10";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Fufle\Database;
|
||||
|
||||
use PDO;
|
||||
|
||||
class ConnectionManager {
|
||||
private static $connection;
|
||||
|
||||
public static function connect(string $connectionString) {
|
||||
self::$connection = new PDO($connectionString);
|
||||
}
|
||||
|
||||
public static function Query(string $query, array|object $params = [], string $output = 'stdObject') {
|
||||
$stmt = self::$connection->prepare($query);
|
||||
$stmt->execute($params);
|
||||
$stmt->setFetchMode(PDO::FETCH_CLASS, $output);
|
||||
return $stmt->fetch();
|
||||
}
|
||||
|
||||
public static function QueryAll(string $query, array|object $params = [], string $output = 'stdObject') {
|
||||
$stmt = self::$connection->prepare($query);
|
||||
$stmt->execute($params);
|
||||
$stmt->setFetchMode(PDO::FETCH_CLASS, $output);
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace Fufle\ORM;
|
||||
|
||||
interface IQuery {
|
||||
public function Get(): string;
|
||||
//public function Insert(): string;
|
||||
//public function Update(): string;
|
||||
public function Delete(): string;
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
namespace Fufle\ORM;
|
||||
|
||||
use Fufle\Database\ConnectionManager;
|
||||
|
||||
abstract class Model {
|
||||
protected static string $primaryKey = 'id';
|
||||
protected static ?string $table = null;
|
||||
protected static ?IQuery $query = null;
|
||||
|
||||
public function getTableName() {
|
||||
return static::$table;
|
||||
}
|
||||
|
||||
public function getPrimaryKey() {
|
||||
return static::$primaryKey;
|
||||
}
|
||||
|
||||
public static function Get(int $id) {
|
||||
return self::Query(
|
||||
static::$query->Get(),
|
||||
[$id]);
|
||||
}
|
||||
|
||||
protected static function Query($sql, $params) {
|
||||
self::Initialize();
|
||||
return ConnectionManager::Query(
|
||||
$sql,
|
||||
$params,
|
||||
get_called_class());
|
||||
}
|
||||
|
||||
protected static function QueryAll($sql, $params) {
|
||||
self::Initialize();
|
||||
return ConnectionManager::QueryAll(
|
||||
$sql,
|
||||
$params,
|
||||
get_called_class());
|
||||
}
|
||||
|
||||
abstract public static function Initialize() {
|
||||
if (static::$table === null) {
|
||||
static::$table = basename(get_called_class()) . 's';
|
||||
}
|
||||
static::Initialize();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
namespace Fufle\ORM;
|
||||
|
||||
class Query {
|
||||
private string $table;
|
||||
private string $primaryKey;
|
||||
public function __construct(Model $model) {
|
||||
$this->table = $model->getTableName();
|
||||
$this->primaryKey = $model->getPrimaryKey();
|
||||
}
|
||||
public function Get(): string {
|
||||
return 'SELECT * FROM '.$this->table.' WHERE '.$this->primaryKey.' = ?';
|
||||
}
|
||||
|
||||
//public function Insert(): string;
|
||||
//public function Update(): string;
|
||||
public function Delete(): string {
|
||||
return 'DELETE FROM '.$this->table.' WHERE '.$this->primaryKey.' = ?';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use Fufle\Database;
|
||||
|
||||
echo SayHello::world();
|
||||
|
||||
$dns = 'pgsql:host=localhost;port=5432;dbname=dynamic_dev;user=test;password=pass123';
|
||||
ConnectionManager::Connect($dns);
|
||||
echo "
|
||||
|
||||
";
|
||||
var_dump(Job::Get(10));
|
||||
echo "
|
||||
|
||||
";
|
Loading…
Reference in New Issue