Skip to content

Commit

Permalink
[ENH] make all request to be executed from attribute table name and b…
Browse files Browse the repository at this point in the history
…e extracted after.
  • Loading branch information
bim-g committed Jan 6, 2024
1 parent e68b00b commit cb2f3f4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
15 changes: 14 additions & 1 deletion controller/exampleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
namespace Wepesi\Controller;


use Wepesi\Core\Application;
use Wepesi\Core\Http\Input;
use Wepesi\Core\Http\Redirect;
use Wepesi\Core\Session;
use Wepesi\Models\Message;
use Wepesi\Models\Roles;
use Wepesi\Models\Users;

class exampleController
{
Expand All @@ -23,7 +27,16 @@ class exampleController
*/
function home()
{
Redirect::to("/");
$users = (new Users())
->include((new Message()))
->include((new Roles()))->findAll();
if (isset($users['exception'])){
Application::dumper($users['exception']);
}else{
Application::dumper($users[0]);

}
// Redirect::to("/");
}

/**
Expand Down
2 changes: 1 addition & 1 deletion router/route.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
$router->get('/', function () {
(new View)->display('/home');
});
$router->get('/home', '\Wepesi\Controller\exampleController#home');
$router->get('/home', [\Wepesi\Controller\exampleController::class,'home']);
//
$router->post('/changelang', [exampleController::class, 'changeLang'])
->middleware([exampleValidation::class, 'changeLang']);
Expand Down
14 changes: 12 additions & 2 deletions src/Core/Orm/DBSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,16 @@ public function offset(int $offset): DBSelect
public function result(): array
{
$this->build();
return (!isset($this->result['exception']) && count($this->include_object) > 0 && count($this->result) > 0) ? $this->formatData($this->result) : $this->result;
$operation_result = $this->result;
if (!isset($this->result['exception']) ) {
if ($this->isCount) {
$this->isCount = false;
} else if (count($this->result) > 0) {
$operation_result = $this->formatData($operation_result);
}
}
$this->include_object = [];
return $operation_result;
}

/**
Expand All @@ -227,6 +236,7 @@ private function build()
*/
private function count_all(): void
{
$this->isCount = true;
$WHERE = $this->where['field'] ?? '';
$params = $this->where['value'] ?? [];
$sql = "SELECT COUNT(*) as count FROM {$this->table} " . $WHERE;
Expand Down Expand Up @@ -293,7 +303,7 @@ protected function formatData(array $result): array
break;
}
}
return $this->buildStructure($result, $parent_entity[0], $children_entity, $other_entity);
return $this->buildStructure($result, $parent_entity[0] ?? $this->table , $children_entity, $other_entity);

} catch (Exception $ex) {
return ['exception' => $ex->getMessage()];
Expand Down
1 change: 1 addition & 0 deletions src/Core/Orm/Provider/DbProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ abstract class DbProvider implements DbContract
* @var array
*/
protected array $include_object;
protected bool $isCount = false;
use QueryExecuter;

/**
Expand Down
32 changes: 23 additions & 9 deletions src/Core/Orm/Traits/QueryExecuter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@

namespace Wepesi\Core\Orm\Traits;

use PDO;
use PDOException;

/**
*
*/
trait QueryExecuter
{
/**
* @param \PDO $pdo
* @param PDO $pdo
* @param string $sql
* @param array $params
* @return array
*/
protected function executeQuery(\PDO $pdo, string $sql, array $params = [], int $last_id = -1): array
protected function executeQuery(PDO $pdo, string $sql, array $params = [], int $last_id = -1): array
{
try {
$data_result = [
Expand All @@ -29,8 +32,8 @@ protected function executeQuery(\PDO $pdo, string $sql, array $params = [], int
'error' => "",
];
$sql_string = explode(' ', strtolower($sql));
if ($sql_string[0] == 'select' && isset($this->include_object) && count($this->include_object) > 0) {
$pdo->setAttribute(\PDO::ATTR_FETCH_TABLE_NAMES, true);
if ($sql_string[0] == 'select' && !$this->isCount) {
$pdo->setAttribute(PDO::ATTR_FETCH_TABLE_NAMES, true);
}

$query = $pdo->prepare($sql);
Expand All @@ -48,12 +51,23 @@ protected function executeQuery(\PDO $pdo, string $sql, array $params = [], int

switch ($sql_string[0]) {
case 'select' :
if (isset($this->include_object) && count($this->include_object) > 0) {
$data_result['result'] = $query->fetchAll(\PDO::FETCH_GROUP);
if ($this->isCount) {
$count_result = $query->fetchAll(PDO::FETCH_OBJ);
if (isset($count_result[0]->{'.count'})) {
array_map(function ($item) {
if ($item->{'.count'}) {
$item->count = $item->{'.count'};
unset($item->{'.count'});
}
return $item;
}, $count_result);
}
$data_result['result'] = $count_result;
$data_result['count'] = $count_result[0]->count;
} else {
$data_result['result'] = $query->fetchAll(\PDO::FETCH_OBJ);
$data_result['result'] = $query->fetchAll(PDO::FETCH_GROUP);
$data_result['count'] = $query->columnCount();
}
$data_result['count'] = $query->columnCount();
break;
case 'insert' :
$last_id = (int)$pdo->lastInsertId();
Expand All @@ -72,7 +86,7 @@ protected function executeQuery(\PDO $pdo, string $sql, array $params = [], int
}
}
return $data_result;
} catch (\PDOException $ex) {
} catch (PDOException $ex) {
$data_result['error'] = $ex->getmessage();
return $data_result;
}
Expand Down

0 comments on commit cb2f3f4

Please sign in to comment.