2 Commits

Author SHA1 Message Date
Gregory Letellier
293eb9e555 cast extended models when loading from database 2025-01-09 12:00:25 +01:00
Gregory Letellier
d961d2a80f ajout model de type readonly pour une requête de select uniquement 2024-06-18 13:02:18 +02:00
3 changed files with 58 additions and 35 deletions

View File

@@ -12,20 +12,27 @@ class Model implements JsonSerializable
protected string $connection = ""; protected string $connection = "";
protected string $pk; protected string $pk;
private bool $new; private bool $new;
private bool $ro;
public function __construct() public function __construct($ro = false)
{ {
$this->new = true; $this->new = true;
$this->values = array(); $this->values = array();
$this->updated = array(); $this->updated = array();
$this->pk = "id"; $this->pk = "id";
$this->ro = $ro;
$this->initTable(); $this->initTable();
} }
private function initTable(): void private function initTable(): void
{ {
if ($this->table == "") { if (!$this->ro) {
$this->table = $this->getDefaultTableName(); if ($this->table == "") {
$this->table = $this->getDefaultTableName();
}
} else {
$this->table = "ReadOnlyModel";
$this->pk = "__idpk";
} }
} }
@@ -77,7 +84,7 @@ class Model implements JsonSerializable
public function find(mixed $key): mixed public function find(mixed $key): mixed
{ {
$qb = $this->newQueryBuilder(); $qb = $this->newQueryBuilder(get_class($this));
$find = $qb->find($key); $find = $qb->find($key);
if ($find !== null) { if ($find !== null) {
$this->fillData($find->getValues()); $this->fillData($find->getValues());
@@ -89,27 +96,29 @@ class Model implements JsonSerializable
public function save(): bool public function save(): bool
{ {
$ret = false; $ret = false;
$qb = $this->newQueryBuilder(); if (!$this->ro) {
if ($this->new) { $qb = $this->newQueryBuilder("");
$query = $qb->getPreparedQuery($this->values); if ($this->new) {
$id = Connection::getInstance($this->connection)->insertQuery($query); $query = $qb->getPreparedQuery($this->values);
if ($id !== false) { $id = Connection::getInstance($this->connection)->insertQuery($query);
$this->values[$this->pk] = $id; if ($id !== false) {
$this->new = false; $this->values[$this->pk] = $id;
$ret = true; $this->new = false;
} $ret = true;
} else { }
if (isset($this->values[$this->pk])) { } else {
if (count($this->updated) > 0) { if (isset($this->values[$this->pk])) {
$data = []; if (count($this->updated) > 0) {
$data[$this->pk] = $this->values[$this->pk]; $data = [];
foreach ($this->updated as $updatecol) { $data[$this->pk] = $this->values[$this->pk];
$data[$updatecol] = $this->values[$updatecol]; foreach ($this->updated as $updatecol) {
} $data[$updatecol] = $this->values[$updatecol];
$query = $qb->getPreparedQuery($data, false); }
$ret = Connection::getInstance($this->connection)->updateQuery($query); $query = $qb->getPreparedQuery($data, false);
if ($ret) { $ret = Connection::getInstance($this->connection)->updateQuery($query);
$this->updated = []; if ($ret) {
$this->updated = [];
}
} }
} }
} }
@@ -117,9 +126,9 @@ class Model implements JsonSerializable
return $ret; return $ret;
} }
public function newQueryBuilder(): QueryBuilder public function newQueryBuilder($className): QueryBuilder
{ {
return new QueryBuilder($this->table, $this->pk, $this->connection); return new QueryBuilder($this->table, $this->pk, $this->connection, $className);
} }
public function __set($name, $value): void public function __set($name, $value): void
@@ -139,10 +148,16 @@ class Model implements JsonSerializable
} }
} }
public static function getClassName()
{
return static::class;
}
public static function q(): QueryBuilder public static function q(): QueryBuilder
{ {
$cls = self::getClassName();
$ist = new static(); $ist = new static();
return $ist->newQueryBuilder(); return $ist->newQueryBuilder($cls);
} }
public function __get($name): mixed public function __get($name): mixed

View File

@@ -10,6 +10,7 @@ class QueryBuilder
protected array $orders; protected array $orders;
protected array $groupby; protected array $groupby;
protected string $table; protected string $table;
protected string $classname;
protected string $connection; protected string $connection;
protected string $pk; protected string $pk;
protected ?int $take; protected ?int $take;
@@ -20,8 +21,9 @@ class QueryBuilder
public const MODE_AND = 0; public const MODE_AND = 0;
public const MODE_OR = 1; public const MODE_OR = 1;
public function __construct($table, $pk = "id", $connection = "") public function __construct($table, $pk = "id", $connection = "", $classname = "")
{ {
$this->classname = $classname;
$this->table = $table; $this->table = $table;
$this->pk = $pk; $this->pk = $pk;
$this->connection = $connection; $this->connection = $connection;
@@ -208,7 +210,12 @@ class QueryBuilder
{ {
$ret = array(); $ret = array();
foreach ($data as $row) { foreach ($data as $row) {
$model = new Model(); if ($this->classname != "") {
$model = new $this->classname();
} else {
$model = new Model();
}
$ret[] = $model->setPk($this->pk)->setTable($this->table)->fillData($row); $ret[] = $model->setPk($this->pk)->setTable($this->table)->fillData($row);
} }
return new Collection($ret); return new Collection($ret);
@@ -235,11 +242,10 @@ class QueryBuilder
$where = $this->getWhere(); $where = $this->getWhere();
$Orwhere = $this->getWhere(QueryBuilder::MODE_OR); $Orwhere = $this->getWhere(QueryBuilder::MODE_OR);
$this->fillData($ret); $this->fillData($ret);
if($where!="" && $Orwhere!="") if ($where != "" && $Orwhere != "") {
{
$Orwhere = " OR " . $Orwhere; $Orwhere = " OR " . $Orwhere;
} }
@@ -256,8 +262,8 @@ class QueryBuilder
} }
private function fillData(Query &$query): void private function fillData(Query &$query): void
{ {
foreach (array_merge($this->wheres,$this->orwheres) as $expression) { foreach (array_merge($this->wheres, $this->orwheres) as $expression) {
if ($expression instanceof Expression) { if ($expression instanceof Expression) {
if ($expression->hasData()) { if ($expression->hasData()) {
$query->addData($expression->getValue()); $query->addData($expression->getValue());

View File

@@ -116,6 +116,8 @@ test("extending model",function()
$row = $qbs->find(1); $row = $qbs->find(1);
expect($row->id)->toBe(1); expect($row->id)->toBe(1);
expect($row->col1)->toBe("test"); expect($row->col1)->toBe("test");
expect($row)->toBeInstanceOf(Test::class);
}); });
test("order by", function(){ test("order by", function(){