Commit 5abe3177 authored by Alexander Butenko's avatar Alexander Butenko

Added orderBy functionality

parent 4e3c1df0
...@@ -42,6 +42,10 @@ class MysqliDb ...@@ -42,6 +42,10 @@ class MysqliDb
* @var array * @var array
*/ */
protected $_whereTypeList; protected $_whereTypeList;
/**
* Dynamic type list for order by condition value
*/
protected $_orderBy = array();
/** /**
* Dynamic type list for table data values * Dynamic type list for table data values
* *
...@@ -97,6 +101,7 @@ class MysqliDb ...@@ -97,6 +101,7 @@ class MysqliDb
protected function reset() protected function reset()
{ {
$this->_where = array(); $this->_where = array();
$this->_orderBy = array();
$this->_bindParams = array(''); // Create the empty 0 index $this->_bindParams = array(''); // Create the empty 0 index
unset($this->_query); unset($this->_query);
unset($this->_whereTypeList); unset($this->_whereTypeList);
...@@ -252,6 +257,21 @@ class MysqliDb ...@@ -252,6 +257,21 @@ class MysqliDb
return $this; return $this;
} }
/**
* This method allows you to specify multiple (method chaining optional) ORDER BY statements for SQL queries.
*
* @uses $MySqliDb->orderBy('id', 'desc')->orderBy('name', 'desc');
*
* @param string $whereProp The name of the database field.
* @param mixed $whereValue The value of the database field.
*
* @return MysqliDb
*/
public function orderBy($orderByField, $orderbyDirection)
{
$this->_orderBy[$orderByField] = $orderbyDirection;
return $this;
}
/** /**
* This methods returns the ID of the last inserted item * This methods returns the ID of the last inserted item
...@@ -379,6 +399,16 @@ class MysqliDb ...@@ -379,6 +399,16 @@ class MysqliDb
$this->_query = rtrim($this->_query, ' AND '); $this->_query = rtrim($this->_query, ' AND ');
} }
// Did the user call the "orderBy" method?
if (!empty ($this->_orderBy)) {
$this->_query .= " order by ";
foreach ($this->_orderBy as $prop => $value) {
// prepares the reset of the SQL query.
$this->_query .= $prop . " " . $value . ", ";
}
$this->_query = rtrim ($this->_query, ', ') . " ";
}
// Determine if is INSERT query // Determine if is INSERT query
if ($hasTableData) { if ($hasTableData) {
$pos = strpos($this->_query, 'INSERT'); $pos = strpos($this->_query, 'INSERT');
......
...@@ -118,3 +118,10 @@ $results = $db ...@@ -118,3 +118,10 @@ $results = $db
->where('title', 'MyTitle') ->where('title', 'MyTitle')
->get('tableName'); ->get('tableName');
``` ```
### Ordering method
```php
$db->orderBy("id","asc");
$db->orderBy("name","Desc");
$results = $db->get('tableName');
```
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment