Compare commits
2 Commits
Author | SHA1 | Date |
---|---|---|
Michal Sieciechowicz | a3d71c7bc5 | |
Michal Sieciechowicz | a3c38a27c7 |
|
@ -1,22 +1,19 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Model;
|
namespace Fufle\Demo\Model;
|
||||||
|
|
||||||
use App\Query\IJobQuery;
|
use Fufle\Demo\Query\IJobQuery;
|
||||||
use App\Query\JobQuery;
|
use Fufle\Demo\Query\JobQuery;
|
||||||
use Fufle\ORM\Model as BaseModel;
|
use Fufle\ORM\Model as BaseModel;
|
||||||
|
|
||||||
class Job extends BaseModel {
|
class Job extends BaseModel {
|
||||||
//private string $table = 'jobs';
|
|
||||||
//protected static ?IJobQuery $query;
|
|
||||||
public int $id;
|
public int $id;
|
||||||
public float $budget;
|
public float $budget;
|
||||||
public static function Initialize() {
|
public static function QueryModel() {
|
||||||
if (static::$query === null) {
|
return new JobQuery( static::class );
|
||||||
static::$query = new JobQuery(self::class);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function GetLast10() {
|
public static function GetLast10() {
|
||||||
return self::QueryAll(self::$query->GetLast10());
|
return self::QueryAll(self::QueryModel()->GetLast10());
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Query;
|
namespace Fufle\Demo\Query;
|
||||||
|
|
||||||
use Fufle\ORM\IQuery as IBaseQuery;
|
use Fufle\ORM\IQuery as IBaseQuery;
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Query;
|
namespace Fufle\Demo\Query;
|
||||||
|
|
||||||
use App\Query\IJobQuery;
|
use Fufle\Demo\Query\IJobQuery;
|
||||||
use Fufle\ORM\Query as BaseQuery;
|
use Fufle\ORM\Query as BaseQuery;
|
||||||
|
|
||||||
class JobQuery extends BaseQuery implements IJobQuery {
|
class JobQuery extends BaseQuery implements IJobQuery {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"name": "Fufle/SiBase2",
|
"name": "fufle/sibase2",
|
||||||
"description": "MicroORM for php8",
|
"description": "MicroORM for php8",
|
||||||
"type": "orm",
|
"type": "orm",
|
||||||
"require": {
|
"require": {
|
||||||
|
@ -16,7 +16,8 @@
|
||||||
"minimum-stability": "dev",
|
"minimum-stability": "dev",
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Fufle\\": "src/Fufle/"
|
"Fufle\\": "src/Fufle/",
|
||||||
|
"Fufle\\Demo\\": "app/"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,33 +7,40 @@ use Fufle\Database\ConnectionManager;
|
||||||
abstract class Model {
|
abstract class Model {
|
||||||
protected static string $primaryKey = 'id';
|
protected static string $primaryKey = 'id';
|
||||||
protected static ?string $table = null;
|
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;
|
return static::$table;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getPrimaryKey() {
|
public static function GetPrimaryKey() {
|
||||||
return static::$primaryKey;
|
return static::$primaryKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function Get(int $id) {
|
public static function Get(int $id) {
|
||||||
self::Check();
|
|
||||||
return self::Query(
|
return self::Query(
|
||||||
static::$query->Get(),
|
static::QueryModel()->Get(),
|
||||||
[$id]);
|
[$id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static function Query(string $sql, array|object $params = []) {
|
protected static function Query(string $sql, array|object $params = []) {
|
||||||
self::Check();
|
|
||||||
return ConnectionManager::Query(
|
return ConnectionManager::Query(
|
||||||
$sql,
|
$sql,
|
||||||
$params,
|
$params,
|
||||||
get_called_class());
|
get_called_class());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static function QueryAll(string $sql, array|object $params = []) {
|
protected static function QueryAll(string $sql, array|object $params = []) {
|
||||||
self::Check();
|
|
||||||
return ConnectionManager::QueryAll(
|
return ConnectionManager::QueryAll(
|
||||||
$sql,
|
$sql,
|
||||||
$params,
|
$params,
|
||||||
|
@ -46,6 +53,4 @@ abstract class Model {
|
||||||
}
|
}
|
||||||
static::Initialize();
|
static::Initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract public static function Initialize();
|
|
||||||
}
|
}
|
|
@ -2,12 +2,14 @@
|
||||||
namespace Fufle\ORM;
|
namespace Fufle\ORM;
|
||||||
|
|
||||||
class Query {
|
class Query {
|
||||||
private string $table;
|
protected string $table;
|
||||||
private string $primaryKey;
|
protected string $primaryKey;
|
||||||
|
|
||||||
public function __construct(string $model) {
|
public function __construct(string $model) {
|
||||||
$this->table = $model::getTableName();
|
$this->table = $model::GetTableName();
|
||||||
$this->primaryKey = $model::getPrimaryKey();
|
$this->primaryKey = $model::GetPrimaryKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Get(): string {
|
public function Get(): string {
|
||||||
return 'SELECT * FROM '.$this->table.' WHERE '.$this->primaryKey.' = ?';
|
return 'SELECT * FROM '.$this->table.' WHERE '.$this->primaryKey.' = ?';
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,19 @@
|
||||||
<?php
|
<?php
|
||||||
require_once __DIR__ . '/../vendor/autoload.php';
|
require_once __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
use App\Model\Job;
|
use Fufle\Demo\Model\Job;
|
||||||
use Fufle\Database\ConnectionManager;
|
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);
|
ConnectionManager::Connect($dns);
|
||||||
echo "
|
echo "
|
||||||
|
|
||||||
";
|
|
||||||
var_dump(Job::Get(10));
|
|
||||||
echo "
|
|
||||||
|
|
||||||
";
|
";
|
||||||
var_dump(Job::GetLast10());
|
var_dump(Job::GetLast10());
|
||||||
echo "
|
echo "
|
||||||
|
|
||||||
|
";
|
||||||
|
var_dump(Job::Get(10));
|
||||||
|
echo "
|
||||||
|
|
||||||
";
|
";
|
Loading…
Reference in New Issue