diff --git a/src/Venom/Core/Database/Entity.php b/src/Venom/Core/Database/Entity.php index 30c1bde..053f5ca 100755 --- a/src/Venom/Core/Database/Entity.php +++ b/src/Venom/Core/Database/Entity.php @@ -20,6 +20,15 @@ abstract class Entity implements JsonSerializable // Override this if you want special fields :) + public function getLoadedFieldValues(): array + { + $keyValue = []; + foreach ($this->loadedFields as $var) { + $keyValue[$var] = $this->$var; + } + return $keyValue; + } + public function getFieldsToWrite(): array { if ($this->fields !== null) { @@ -33,9 +42,10 @@ abstract class Entity implements JsonSerializable unset($vars[$key]); } } + if (!$allLoaded) { foreach ($vars as $key => $var) { - if (!in_array($var, $this->loadedFields)) { + if (!in_array($key, $this->loadedFields)) { unset($vars[$key]); } } @@ -104,7 +114,7 @@ abstract class Entity implements JsonSerializable public function jsonSerialize(): array { - return $this->getFieldsToWrite(); + return $this->getLoadedFieldValues(); } public function preSave() diff --git a/src/Venom/Core/Database/EntityManager.php b/src/Venom/Core/Database/EntityManager.php index 24d6d4f..9aef7d5 100755 --- a/src/Venom/Core/Database/EntityManager.php +++ b/src/Venom/Core/Database/EntityManager.php @@ -50,10 +50,10 @@ class EntityManager return null; } - public function saveAll() + public function saveAll(): bool { if (count($this->entities) === 0) { - return; + return true; } try { $this->db->start(); @@ -64,10 +64,12 @@ class EntityManager } catch (Exception $ex) { trigger_error($ex->getMessage()); $this->db->rollBack(); + return false; } + return true; } - public function deleteEntities() + public function deleteEntities(): bool { try { $this->db->start(); @@ -78,7 +80,9 @@ class EntityManager } catch (Exception $ex) { trigger_error($ex->getMessage()); $this->db->rollBack(); + return false; } + return true; } public function clearAll() @@ -118,6 +122,11 @@ class EntityManager return $this->entities; } + public function getFirst(): ?Entity + { + return $this->entities[0] ?? null; + } + private function addEntities(array $entities) { foreach ($entities as $entity) { diff --git a/src/modules/User/Controller/UserAPIController.php b/src/modules/User/Controller/UserAPIController.php index 8addaee..a4c7000 100755 --- a/src/modules/User/Controller/UserAPIController.php +++ b/src/modules/User/Controller/UserAPIController.php @@ -22,32 +22,27 @@ class UserAPIController public function getById($id) { - $d = UserController::getById($id, ["id", "username", "firstname", "lastname", "email", "isActive"]); - AdminHelper::sendResponse($d); + $entityManager = EntityManager::create(User::class); + $easyQuery = new EasyQuery(User::$tableName, ["id", "username", "firstname", "lastname", "email", "isActive"]); + $easyQuery->where("id", $id); + $entityManager->load($easyQuery); + AdminHelper::sendResponse($entityManager->getFirst()); } public function update($id) { - $original = UserController::getById($id); + $entityManager = EntityManager::create(User::class); + $easyQuery = new EasyQuery(User::$tableName, ["id", "username", "firstname", "lastname", "email", "isActive"]); + $easyQuery->where("id", $id); + $entityManager->load($easyQuery); + /** @var User|null $original */ + $original = $entityManager->getFirst(); if ($original == null) { AdminHelper::sendStatus(false, "User not Found"); } - $args = ArgumentHandler::get(); - $data = []; - $d = $original->getData(); - foreach ($d as $key => $item) { - if ($args->hasPostItem($key)) { - $val = $args->getPostItem($key); - if ($val != $item) { - $data[$key] = $val; - } - } - } parse_str(file_get_contents('php://input'), $_PUT); - var_dump(array_keys($_PUT)); - //var_dump($data, $d, $_POST); - // $args->getPostItem("username")//UPDATE users SET lastname='Doe', firstname='' WHERE id=2 - AdminHelper::sendStatus(UserController::update($id, $data)); +/* var_dump(array_keys($_PUT));*/ + AdminHelper::sendStatus($entityManager->saveAll()); } public function delete($id) diff --git a/src/modules/User/Controller/UserController.php b/src/modules/User/Controller/UserController.php deleted file mode 100755 index dba2e69..0000000 --- a/src/modules/User/Controller/UserController.php +++ /dev/null @@ -1,36 +0,0 @@ -getOne($sel, [ - ':id' => $id - ]); - } - - public static function get(array $fields = ["*"]): array - { - return DatabaseHandler::get()->getAll(DatabaseHandler::createEasySelect($fields, "users")); - } - - public static function update($id, array $values = []): bool - { - if (count($values) === 0) { - return false; - } - return DatabaseHandler::get()->execute(...DatabaseHandler::getUpdateString($values, "users", "WHERE id = :id")); - } -} \ No newline at end of file diff --git a/src/modules/User/module.php b/src/modules/User/module.php index c060b78..3f8d42a 100755 --- a/src/modules/User/module.php +++ b/src/modules/User/module.php @@ -23,7 +23,7 @@ $venom->registerModule([ ], "1" => [ Route::GET => 'getById', - Route::POST => 'insert', + Route::POST => 'update', Route::PUT => 'update', Route::DELETE => 'delete' ] diff --git a/tpl/admin/jsTemplates/userEdit.tpl b/tpl/admin/jsTemplates/userEdit.tpl index ca7b8e0..7f4a1f0 100755 --- a/tpl/admin/jsTemplates/userEdit.tpl +++ b/tpl/admin/jsTemplates/userEdit.tpl @@ -1,65 +1,34 @@