ajout model de type readonly pour une requête de select uniquement
This commit is contained in:
@@ -12,20 +12,30 @@ 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";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,27 +99,30 @@ 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) {
|
{
|
||||||
$query = $qb->getPreparedQuery($this->values);
|
$qb = $this->newQueryBuilder();
|
||||||
$id = Connection::getInstance($this->connection)->insertQuery($query);
|
if ($this->new) {
|
||||||
if ($id !== false) {
|
$query = $qb->getPreparedQuery($this->values);
|
||||||
$this->values[$this->pk] = $id;
|
$id = Connection::getInstance($this->connection)->insertQuery($query);
|
||||||
$this->new = false;
|
if ($id !== false) {
|
||||||
$ret = true;
|
$this->values[$this->pk] = $id;
|
||||||
}
|
$this->new = false;
|
||||||
} else {
|
$ret = true;
|
||||||
if (isset($this->values[$this->pk])) {
|
}
|
||||||
if (count($this->updated) > 0) {
|
} else {
|
||||||
$data = [];
|
if (isset($this->values[$this->pk])) {
|
||||||
$data[$this->pk] = $this->values[$this->pk];
|
if (count($this->updated) > 0) {
|
||||||
foreach ($this->updated as $updatecol) {
|
$data = [];
|
||||||
$data[$updatecol] = $this->values[$updatecol];
|
$data[$this->pk] = $this->values[$this->pk];
|
||||||
}
|
foreach ($this->updated as $updatecol) {
|
||||||
$query = $qb->getPreparedQuery($data, false);
|
$data[$updatecol] = $this->values[$updatecol];
|
||||||
$ret = Connection::getInstance($this->connection)->updateQuery($query);
|
}
|
||||||
if ($ret) {
|
$query = $qb->getPreparedQuery($data, false);
|
||||||
$this->updated = [];
|
$ret = Connection::getInstance($this->connection)->updateQuery($query);
|
||||||
|
if ($ret) {
|
||||||
|
$this->updated = [];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user