This commit is contained in:
2022-08-20 22:17:17 +02:00
parent 58c905acf5
commit 35d7e4000a
29 changed files with 619 additions and 131 deletions

View File

@@ -76,6 +76,18 @@ abstract class DatabaseEntity {
return true;
}
public function insert(SQL $sql): bool {
$handler = self::getHandler($sql);
$res = $handler->insert($this);
if ($res === false) {
return false;
} else if ($this->id === null) {
$this->id = $res;
}
return true;
}
public function delete(SQL $sql): bool {
$handler = self::getHandler($sql);
if ($this->id === null) {

View File

@@ -311,10 +311,7 @@ class DatabaseEntityHandler {
return $query->execute();
}
public function insertOrUpdate(DatabaseEntity $entity, ?array $columns = null) {
$id = $entity->getId();
$action = $id === null ? "insert" : "update";
private function prepareRow(DatabaseEntity $entity, string $action, ?array $columns = null) {
$row = [];
foreach ($this->columns as $propertyName => $column) {
if ($columns && !in_array($column->getName(), $columns)) {
@@ -331,7 +328,10 @@ class DatabaseEntityHandler {
} else if (!$this->columns[$propertyName]->notNull()) {
$value = null;
} else {
if ($action !== "update") {
$defaultValue = self::getAttribute($property, DefaultValue::class);
if ($defaultValue) {
$value = $defaultValue->getValue();
} else if ($action !== "update") {
$this->logger->error("Cannot $action entity: property '$propertyName' was not initialized yet.");
return false;
} else {
@@ -342,29 +342,62 @@ class DatabaseEntityHandler {
$row[$column->getName()] = $value;
}
return $row;
}
public function update(DatabaseEntity $entity, ?array $columns = null) {
$row = $this->prepareRow($entity, "update", $columns);
if ($row === false) {
return false;
}
$entity->preInsert($row);
$query = $this->sql->update($this->tableName)
->where(new Compare($this->tableName . ".id", $entity->getId()));
foreach ($row as $columnName => $value) {
$query->set($columnName, $value);
}
return $query->execute();
}
public function insert(DatabaseEntity $entity) {
$row = $this->prepareRow($entity, "insert");
if ($row === false) {
return false;
}
$entity->preInsert($row);
// insert with id?
$entityId = $entity->getId();
if ($entityId !== null) {
$row["id"] = $entityId;
}
if ($id === null) {
$res = $this->sql->insert($this->tableName, array_keys($row))
->addRow(...array_values($row))
->returning("id")
->execute();
$query = $this->sql->insert($this->tableName, array_keys($row))
->addRow(...array_values($row));
if ($res !== false) {
return $this->sql->getLastInsertId();
} else {
return false;
}
// return id if its auto-generated
if ($entityId === null) {
$query->returning("id");
}
$res = $query->execute();
if ($res !== false) {
return $this->sql->getLastInsertId();
} else {
$query = $this->sql->update($this->tableName)
->where(new Compare($this->tableName . ".id", $id));
return false;
}
}
foreach ($row as $columnName => $value) {
$query->set($columnName, $value);
}
return $query->execute();
public function insertOrUpdate(DatabaseEntity $entity, ?array $columns = null) {
$id = $entity->getId();
if ($id === null) {
return $this->insert($entity);
} else {
return $this->update($entity, $columns);
}
}

View File

@@ -2,6 +2,7 @@
namespace Objects\DatabaseEntity {
use Driver\SQL\SQL;
use Objects\DatabaseEntity\Attribute\MaxLength;
use Objects\DatabaseEntity\Attribute\Transient;
use Objects\lang\LanguageModule;
@@ -14,15 +15,13 @@ namespace Objects\DatabaseEntity {
#[MaxLength(5)] private string $code;
#[MaxLength(32)] private string $name;
#[Transient] private array $modules;
#[Transient] protected array $entries;
#[Transient] private array $modules = [];
#[Transient] protected array $entries = [];
public function __construct(int $id, string $code, string $name) {
parent::__construct($id);
$this->code = $code;
$this->name = $name;
$this->entries = array();
$this->modules = array();
}
public function getCode(): string {
@@ -42,9 +41,11 @@ namespace Objects\DatabaseEntity {
$module = new $module();
}
$moduleEntries = $module->getEntries($this->code);
$this->entries = array_merge($this->entries, $moduleEntries);
$this->modules[] = $module;
if (!in_array($module, $this->modules)) {
$moduleEntries = $module->getEntries($this->code);
$this->entries = array_merge($this->entries, $moduleEntries);
$this->modules[] = $module;
}
}
public function translate(string $key): string {
@@ -89,6 +90,10 @@ namespace Objects\DatabaseEntity {
return new Language(1, "en_US", "American English");
}
public function getEntries(): array {
return $this->entries;
}
}
}

View File

@@ -105,12 +105,6 @@ class Session extends DatabaseEntity {
);
}
public function insert(bool $stayLoggedIn = false): bool {
$this->stayLoggedIn = $stayLoggedIn;
$this->active = true;
return $this->update();
}
public function destroy(): bool {
session_destroy();
$this->active = false;