VENOM: Entity fixed JSON
EntityManger added return value EntityManager added getFirst Fixed Some Admin shit :)
This commit is contained in:
parent
eb6770204a
commit
6523e77742
7 changed files with 61 additions and 116 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -1,36 +0,0 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace Modules\User\Controller;
|
||||
|
||||
|
||||
use Venom\Core\DatabaseHandler;
|
||||
use Venom\Entities\DatabaseObject;
|
||||
|
||||
class UserController
|
||||
{
|
||||
|
||||
public static function getById($id, array $fields = ["*"]): ?DatabaseObject
|
||||
{
|
||||
if (empty($id)) {
|
||||
return null;
|
||||
}
|
||||
$sel = DatabaseHandler::createEasySelect($fields, "users") . " WHERE id = :id";
|
||||
return DatabaseHandler::get()->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"));
|
||||
}
|
||||
}
|
||||
|
|
@ -23,7 +23,7 @@ $venom->registerModule([
|
|||
],
|
||||
"1" => [
|
||||
Route::GET => 'getById',
|
||||
Route::POST => 'insert',
|
||||
Route::POST => 'update',
|
||||
Route::PUT => 'update',
|
||||
Route::DELETE => 'delete'
|
||||
]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue