Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] error message while using asc or desc without order by. #161

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
$app_root = appDirSeparator(dirname(__DIR__));
foreach ($autoload as $src) {
$folder = $app_root . '/' . $src;
$dirs = getSubDirectories($folder);
$directories = getSubDirectories($folder);
$classFile = extractNamespace($class);
foreach ($dirs as $dir) {
foreach ($directories as $dir) {
$file = $dir . '/' . checkFileExtension($classFile);
if (is_file($file)) {
require_once "$file";
Expand Down
2 changes: 1 addition & 1 deletion config/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function serverDomain(): object
* Project autoload register
*/
require_once $ROOT_DIR . '/config/autoload.php';
require_once $ROOT_DIR . '/vendor/autoload.php';
//require_once $ROOT_DIR . '/vendor/autoload.php';
bim-g marked this conversation as resolved.
Show resolved Hide resolved
/**
* Project Utils
*/
Expand Down
37 changes: 20 additions & 17 deletions src/Core/Orm/DBSelect.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?php
/*
* Copyright (c) 2024. Wepesi Dev Framework
*/

namespace Wepesi\Core\Orm;

use Exception;
use PDO;
use Wepesi\Core\Escape;
use Wepesi\Core\Orm\Provider\DbProvider;
use Wepesi\Core\Orm\Traits\DBWhereCondition;
Expand Down Expand Up @@ -44,14 +49,11 @@ class DBSelect extends DbProvider
* @var string
*/
private string $_offset;

/**
* @var string
*/
private string $_dsc;
/**
* @var string
*/
private string $_asc;
private string $ascending;
/**
* @var string
*/
Expand All @@ -61,11 +63,11 @@ class DBSelect extends DbProvider

/**
*
* @param \PDO $pdo
* @param PDO $pdo
* @param string $table
* @param string|null $action
*/
public function __construct(\PDO $pdo, string $table, string $action = null)
public function __construct(PDO $pdo, string $table, string $action = null)
{
$this->table = $table;
$this->pdo = $pdo;
Expand All @@ -79,8 +81,7 @@ public function __construct(\PDO $pdo, string $table, string $action = null)
$this->_fields = ['keys' => '*'];
$this->_limit = '';
$this->_offset = '';
$this->_dsc = '';
$this->_asc = '';
$this->ascending = '';
$this->_between = '';
$this->include_object = [];
}
Expand Down Expand Up @@ -163,8 +164,7 @@ public function random(): DBSelect
*/
public function ASC(): DBSelect
{
$this->_asc = ' ASC ';
$this->_dsc = '';
$this->ascending = ' ASC ';
return $this;
}

Expand All @@ -174,8 +174,7 @@ public function ASC(): DBSelect
*/
public function DESC(): DBSelect
{
$this->_asc = '';
$this->_dsc = ' DESC ';
$this->ascending = ' DESC ';
return $this;
}

Expand Down Expand Up @@ -249,8 +248,12 @@ private function select()
}
$params = $this->where['value'] ?? [];
//
$sql = "SELECT {$fields} FROM {$this->table} " . $WHERE . $this->groupBY . $this->orderBy . $this->_asc . $this->_dsc . $this->_limit . $this->_offset;
$this->query($sql, $params);
if ($this->orderBy == '' && $this->ascending != '') {
$this->result['exception'] = 'You should provide the order by which field name to which field you to apply the `' . $this->ascending . '`.';
} else {
$sql = "SELECT {$fields} FROM {$this->table} " . $WHERE . $this->groupBY . $this->orderBy . $this->ascending . $this->_limit . $this->_offset;
$this->query($sql, $params);
}
}

/**
Expand Down Expand Up @@ -292,7 +295,7 @@ protected function formatData(array $result): array
}
return $this->buildStructure($result, $parent_entity[0], $children_entity, $other_entity);

} catch (\Exception $ex) {
} catch (Exception $ex) {
return ['exception' => $ex->getMessage()];
}
}
Expand Down Expand Up @@ -347,7 +350,7 @@ protected function buildStructure($result, string $parent_entity, array $childre
$data_result[] = (object)$parent_table;
}
return $data_result;
} catch (\Exception $ex) {
} catch (Exception $ex) {
return ['exception' => $ex->getMessage()];
}
}
Expand Down