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

Added orderBy functionality

parent 4e3c1df0
......@@ -42,6 +42,10 @@ class MysqliDb
* @var array
*/
protected $_whereTypeList;
/**
* Dynamic type list for order by condition value
*/
protected $_orderBy = array();
/**
* Dynamic type list for table data values
*
......@@ -97,6 +101,7 @@ class MysqliDb
protected function reset()
{
$this->_where = array();
$this->_orderBy = array();
$this->_bindParams = array(''); // Create the empty 0 index
unset($this->_query);
unset($this->_whereTypeList);
......@@ -252,6 +257,21 @@ class MysqliDb
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
......@@ -379,6 +399,16 @@ class MysqliDb
$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
if ($hasTableData) {
$pos = strpos($this->_query, 'INSERT');
......
......@@ -118,3 +118,10 @@ $results = $db
->where('title', 'MyTitle')
->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