This commit is contained in:
Michal Sieciechowicz 2021-03-01 16:06:02 +01:00
parent e79ee148dd
commit a3c38a27c7
7 changed files with 42 additions and 37 deletions

View File

@ -1,22 +1,19 @@
<?php
namespace App\Model;
namespace Fufle\Demo\Model;
use App\Query\IJobQuery;
use App\Query\JobQuery;
use Fufle\Demo\Query\IJobQuery;
use Fufle\Demo\Query\JobQuery;
use Fufle\ORM\Model as BaseModel;
class Job extends BaseModel {
//private string $table = 'jobs';
//protected static ?IJobQuery $query;
public int $id;
public float $budget;
public static function Initialize() {
if (static::$query === null) {
static::$query = new JobQuery(self::class);
}
public static function QueryModel() {
return new JobQuery( static::class );
}
public static function GetLast10() {
return self::QueryAll(self::$query->GetLast10());
return self::QueryAll(self::QueryModel()->GetLast10());
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace App\Query;
namespace Fufle\Demo\Query;
use Fufle\ORM\IQuery as IBaseQuery;

View File

@ -1,8 +1,8 @@
<?php
namespace App\Query;
namespace Fufle\Demo\Query;
use App\Query\IJobQuery;
use Fufle\Demo\Query\IJobQuery;
use Fufle\ORM\Query as BaseQuery;
class JobQuery extends BaseQuery implements IJobQuery {

View File

@ -1,5 +1,5 @@
{
"name": "Fufle/SiBase2",
"name": "fufle/sibase2",
"description": "MicroORM for php8",
"type": "orm",
"require": {
@ -16,7 +16,8 @@
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"Fufle\\": "src/Fufle/"
"Fufle\\": "src/Fufle/",
"Fufle\\Demo\\": "app/"
}
}
}

View File

@ -7,25 +7,33 @@ use Fufle\Database\ConnectionManager;
abstract class Model {
protected static string $primaryKey = 'id';
protected static ?string $table = null;
protected static ?IQuery $query = null;
private static ?IQuery $query = null;
public static function getTableName() {
protected static function QueryModel() {
if (self::$query === null && self::class !== static::class) {
self::$query = static::QueryModel();
}
return self::$query;
}
public static function GetTableName() {
if (static::$table === null) {
static::$table = basename(str_replace('\\', '/', get_called_class())) . 's';
}
return static::$table;
}
public static function getPrimaryKey() {
public static function GetPrimaryKey() {
return static::$primaryKey;
}
public static function Get(int $id) {
self::Check();
return self::Query(
static::$query->Get(),
static::QueryModel()->Get(),
[$id]);
}
protected static function Query(string $sql, array|object $params = []) {
self::Check();
return ConnectionManager::Query(
$sql,
$params,
@ -33,7 +41,6 @@ abstract class Model {
}
protected static function QueryAll(string $sql, array|object $params = []) {
self::Check();
return ConnectionManager::QueryAll(
$sql,
$params,
@ -46,6 +53,4 @@ abstract class Model {
}
static::Initialize();
}
abstract public static function Initialize();
}

View File

@ -4,10 +4,12 @@ namespace Fufle\ORM;
class Query {
private string $table;
private string $primaryKey;
public function __construct(string $model) {
$this->table = $model::getTableName();
$this->primaryKey = $model::getPrimaryKey();
$this->table = $model::GetTableName();
$this->primaryKey = $model::GetPrimaryKey();
}
public function Get(): string {
return 'SELECT * FROM '.$this->table.' WHERE '.$this->primaryKey.' = ?';
}

View File

@ -1,19 +1,19 @@
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use App\Model\Job;
use Fufle\Demo\Model\Job;
use Fufle\Database\ConnectionManager;
$dns = 'pgsql:host=localhost;port=5432;dbname=dynamic_dev;user=test;password=pass123';
$dns = 'pgsql:host=localhost;port=5432;dbname=dynamic;user=dynamic;password=password123';
ConnectionManager::Connect($dns);
echo "
";
var_dump(Job::Get(10));
echo "
";
var_dump(Job::GetLast10());
echo "
";
var_dump(Job::Get(10));
echo "
";