Making getRecords more benign
This commit is contained in:
+40
-5
@@ -114,7 +114,7 @@ class DBInstance {
|
|||||||
*
|
*
|
||||||
* @return stdClass A object with the record info
|
* @return stdClass A object with the record info
|
||||||
*/
|
*/
|
||||||
public static function getRecord($table, $data = null, $select = Array('*'), $instance = 'default'){
|
public static function getRecord($table, $data = Array(), $select = Array('*'), $instance = 'default'){
|
||||||
$select = implode(', ', $select);
|
$select = implode(', ', $select);
|
||||||
|
|
||||||
$conditions = "";
|
$conditions = "";
|
||||||
@@ -141,9 +141,11 @@ class DBInstance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$conditions = rtrim($conditions, 'AND ');
|
$conditions = rtrim($conditions, 'AND ');
|
||||||
$sql = "SELECT $select FROM {".$table."} WHERE $conditions";
|
if($conditions == ""){ $conditions = "1 = 1"; }
|
||||||
|
|
||||||
return self::queryOne($sql , $params, $instance)[0];
|
$sql = "SELECT $select FROM {".$table."} WHERE $conditions LIMIT 1";
|
||||||
|
|
||||||
|
return self::queryOne($sql , $params, $instance);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,10 +189,37 @@ class DBInstance {
|
|||||||
*
|
*
|
||||||
* @return stdClass A object with the record info
|
* @return stdClass A object with the record info
|
||||||
*/
|
*/
|
||||||
public static function getRecords($table, $data = null, $select = Array('*'), $instance = 'default'){
|
public static function getRecords($table, $data = null, $select = Array('*'), $limits = Array(), $instance = 'default'){
|
||||||
|
$pointer = self::getRecordsPointer($table, $data, $select, $limits, $instance);
|
||||||
|
|
||||||
|
$elements = Array();
|
||||||
|
foreach ($pointer as $point){
|
||||||
|
$key = array_keys((array)$point)[0];
|
||||||
|
$elements[ ((array)$point)[$key] ] = $point;
|
||||||
|
}
|
||||||
|
return $elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getRecordsPointer
|
||||||
|
*
|
||||||
|
* This method will get records from the database
|
||||||
|
*
|
||||||
|
* @ctag DBInstance::getRecordsPointer($table);
|
||||||
|
* @ctag DBInstance::getRecordsPointer($table, []);
|
||||||
|
*
|
||||||
|
* @param String $table The table to be searched
|
||||||
|
* @param Array $data Pass the info to be replaced on the query_prepare
|
||||||
|
* @param Array $select Info to be put on the SELECT part of the query
|
||||||
|
* @param String $instance Select the connection to execute
|
||||||
|
*
|
||||||
|
* @return stdClass A object with the record info
|
||||||
|
*/
|
||||||
|
public static function getRecordsPointer($table, $data = Array(), $select = Array('*'), $limits = Array(), $instance = 'default'){
|
||||||
$select = implode(', ', $select);
|
$select = implode(', ', $select);
|
||||||
|
|
||||||
$conditions = "";
|
$conditions = "";
|
||||||
|
$limits_sql = "";
|
||||||
$params = Array();
|
$params = Array();
|
||||||
|
|
||||||
foreach ($data as $key => $cond){
|
foreach ($data as $key => $cond){
|
||||||
@@ -213,8 +242,14 @@ class DBInstance {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foreach ($limits as $key => $value) {
|
||||||
|
$limits_sql .= "$key $value ";
|
||||||
|
}
|
||||||
|
|
||||||
$conditions = rtrim($conditions, 'AND ');
|
$conditions = rtrim($conditions, 'AND ');
|
||||||
$sql = "SELECT $select FROM {".$table."} WHERE $conditions";
|
if($conditions == ""){ $conditions = "1 = 1"; $params = false; }
|
||||||
|
|
||||||
|
$sql = "SELECT $select FROM {".$table."} WHERE $conditions $limits_sql";
|
||||||
|
|
||||||
return self::query($sql, $params, $instance);
|
return self::query($sql, $params, $instance);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user