Skip to content

Commit

Permalink
[skip ci] [DAO] adds orderBy for DAO
Browse files Browse the repository at this point in the history
  • Loading branch information
jcheron committed Apr 29, 2023
1 parent 5a9d73f commit 139e5a9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 35 deletions.
80 changes: 49 additions & 31 deletions src/Ubiquity/orm/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* This class is part of Ubiquity
*
* @author jcheron <myaddressmail@gmail.com>
* @version 1.2.6
* @version 1.2.7
*
*/
class DAO {
Expand Down Expand Up @@ -78,7 +78,7 @@ public static function getAll($className, $condition = '', $included = true, $pa
* @param boolean $useCache use the active cache if true
* @return array
*/
public static function getAllByIds($className, $keyValues = [ ], $included = true, $condition = '', $useCache = NULL) {
public static function getAllByIds($className, $keyValues = [ ], $included = true, $condition = '', $useCache = NULL): array {
$db = self::getDb ( $className );
$key = OrmUtils::getFirstKey ( $className );
$countK = \count ( $keyValues );
Expand All @@ -97,6 +97,22 @@ public static function paginate($className, $page = 1, $rowsPerPage = 20, $condi
return self::getAll ( $className, ($condition ?? '1=1') . ' LIMIT ' . $rowsPerPage . ' OFFSET ' . (($page - 1) * $rowsPerPage), $included );
}

/**
* Returns an array of $className objects from the database ordered by $field
*
* @param string $className class name of the model to load
* @param string $field field to order by
* @param string $order order of the sort (ASC or DESC)
* @param string $condition Part following the WHERE of an SQL statement
* @param boolean $included if true, loads associate members with associations
* @param null $parameters parameters to bind
* @param null $useCache use the active cache if true
* @return array
*/
public static function orderBy(string $className, string $field, string $order = 'ASC', string $condition = '', bool $included = true, $parameters = null, $useCache = NULL): array {
return self::getAll ( $className, ($condition ?? '1=1') . ' ORDER BY ' . $field . ' ' . $order, $included, $parameters, $useCache );
}

public static function getRownum($className, $ids) {
$tableName = OrmUtils::getTableName ( $className );
$db = self::getDb ( $className );
Expand Down Expand Up @@ -138,7 +154,7 @@ public static function count($className, $condition = '', $parameters = null) {
* @param array|null $parameters The query parameters
* @return boolean
*/
public static function exists($className, $condition = '', $parameters = null) {
public static function exists($className, $condition = '', $parameters = null): bool {
$tableName = OrmUtils::getTableName ( $className );
if ($condition != '') {
$condition = SqlUtils::checkWhere($condition);
Expand All @@ -148,16 +164,17 @@ public static function exists($className, $condition = '', $parameters = null) {
return (1 == $db->prepareAndFetchColumn ( "SELECT EXISTS(SELECT 1 FROM {$quote}{$tableName}{$quote}{$condition})", $parameters ));
}

/**
* Returns an instance of $className from the database, from $keyvalues values of the primary key or with a condition
*
* @param String $className complete classname of the model to load
* @param array|string $condition condition or primary key values
* @param boolean|array $included if true, charges associate members with association
* @param array|null $parameters the request parameters
* @param boolean|null $useCache use cache if true
* @return object the instance loaded or null if not found
*/
/**
* Returns an instance of $className from the database, from $keyvalues values of the primary key or with a condition
*
* @param String $className complete classname of the model to load
* @param array|string $condition condition or primary key values
* @param boolean|array $included if true, charges associate members with association
* @param array|null $parameters the request parameters
* @param boolean|null $useCache use cache if true
* @return object the instance loaded or null if not found
* @throws DAOException
*/
public static function getOne($className, $condition, $included = true, $parameters = null, $useCache = NULL) {
$db = self::getDb ( $className );
$conditionParser = new ConditionParser ();
Expand Down Expand Up @@ -196,20 +213,21 @@ protected static function getConditionParser($className, $keyValues): ConditionP
return self::$conditionParsers [$className];
}

/**
* Establishes the connection to the database using the past parameters
*
* @param string $offset
* @param string $wrapper
* @param string $dbType
* @param string $dbName
* @param string $serverName
* @param string $port
* @param string $user
* @param string $password
* @param array $options
* @param boolean $cache
*/
/**
* Establishes the connection to the database using the past parameters
*
* @param string $offset
* @param string $wrapper
* @param string $dbType
* @param string $dbName
* @param string $serverName
* @param string $port
* @param string $user
* @param string $password
* @param array $options
* @param boolean $cache
* @throws DAOException|\Ubiquity\exceptions\CacheException
*/
public static function connect($offset, $wrapper, $dbType, $dbName, $serverName = '127.0.0.1', $port = '3306', $user = 'root', $password = '', $options = [ ], $cache = false) {
self::$db [$offset] = new Database ( $wrapper, $dbType, $dbName, $serverName, $port, $user, $password, $options, $cache, self::$pool );
try {
Expand Down Expand Up @@ -241,17 +259,17 @@ public static function getDbOffset(&$config, $offset = null) {
*
* @return boolean
*/
public static function isConnected($offset = 'default') {
public static function isConnected($offset = 'default'): bool {
$db = self::$db [$offset] ?? false;
return $db && ($db instanceof Database) && $db->isConnected ();
return ($db instanceof Database) && $db->isConnected ();
}

/**
* Sets the transformer operation
*
* @param string $op
*/
public static function setTransformerOp($op) {
public static function setTransformerOp(string $op) {
self::$transformerOp = $op;
}

Expand Down Expand Up @@ -298,7 +316,7 @@ public static function getDatabase($offset = 'default') {
return self::$db [$offset]??null;
}

public static function getDatabases() {
public static function getDatabases(): array {
$config = Startup::getConfig ();
if (isset ( $config ['database'] )) {
if (isset ( $config ['database'] ['dbName'] )) {
Expand Down
23 changes: 19 additions & 4 deletions src/Ubiquity/orm/repositories/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* This class is part of Ubiquity
*
* @author jc
* @version 1.0.2
* @version 1.0.3
*
*/
abstract class AbstractRepository {
Expand All @@ -32,6 +32,21 @@ public function all(string $condition = '', $included = false, array $parameters
return DAO::getAll ( $this->getModel (), $condition, $included, $parameters, $useCache );
}

/**
* Load all instances with order.
*
* @param string $field The field to order by
* @param string $order The order (ASC, DESC)
* @param string $condition The condition
* @param bool|array $included The relations to include
* @param array $parameters The parameters for the condition
* @param bool $useCache If true, use the cache
* @return array
*/
public function orderBy(string $field, string $order='ASC', string $condition = '', $included = false, array $parameters = [ ], bool $useCache = false): array {
return DAO::orderBy($this->getModel(), $field, $order, $condition, $included, $parameters, $useCache);
}

/**
* Load one instance by id.
*
Expand Down Expand Up @@ -66,7 +81,7 @@ public function one(string $condition = '', $included = true, array $parameters
* @return bool
* @throws \Exception
*/
public function insert(object $instance, $insertMany = false): bool {
public function insert(object $instance, bool $insertMany = false): bool {
return DAO::insert ( $instance, $insertMany );
}

Expand All @@ -77,7 +92,7 @@ public function insert(object $instance, $insertMany = false): bool {
* @param bool $insertMany
* @return bool
*/
public function update(object $instance, $insertMany = false): bool {
public function update(object $instance, bool $insertMany = false): bool {
return DAO::update ( $instance, $insertMany );
}

Expand All @@ -88,7 +103,7 @@ public function update(object $instance, $insertMany = false): bool {
* @param bool $insertMany
* @return bool|int
*/
public function save(object $instance, $insertMany = false) {
public function save(object $instance, bool $insertMany = false) {
return DAO::save ( $instance, $insertMany );
}

Expand Down

0 comments on commit 139e5a9

Please sign in to comment.