Commit 9bf6ab7d authored by Alexander Butenko's avatar Alexander Butenko

Added groupBy method

parent 5abe3177
...@@ -46,6 +46,10 @@ class MysqliDb ...@@ -46,6 +46,10 @@ class MysqliDb
* Dynamic type list for order by condition value * Dynamic type list for order by condition value
*/ */
protected $_orderBy = array(); protected $_orderBy = array();
/**
* Dynamic type list for group by condition value
*/
protected $_groupBy = array();
/** /**
* Dynamic type list for table data values * Dynamic type list for table data values
* *
...@@ -262,8 +266,8 @@ class MysqliDb ...@@ -262,8 +266,8 @@ class MysqliDb
* *
* @uses $MySqliDb->orderBy('id', 'desc')->orderBy('name', 'desc'); * @uses $MySqliDb->orderBy('id', 'desc')->orderBy('name', 'desc');
* *
* @param string $whereProp The name of the database field. * @param string $orderByField The name of the database field.
* @param mixed $whereValue The value of the database field. * @param mixed $orderByDirection Order direction.
* *
* @return MysqliDb * @return MysqliDb
*/ */
...@@ -273,6 +277,21 @@ class MysqliDb ...@@ -273,6 +277,21 @@ class MysqliDb
return $this; return $this;
} }
/**
* This method allows you to specify multiple (method chaining optional) GROUP BY statements for SQL queries.
*
* @uses $MySqliDb->orderBy('id', 'desc')->groupBy('name', 'desc');
*
* @param string $groupByField The name of the database field.
*
* @return MysqliDb
*/
public function groupBy($groupByField)
{
$this->_groupBy[] = $groupByField;
return $this;
}
/** /**
* This methods returns the ID of the last inserted item * This methods returns the ID of the last inserted item
* *
...@@ -399,9 +418,19 @@ class MysqliDb ...@@ -399,9 +418,19 @@ class MysqliDb
$this->_query = rtrim($this->_query, ' AND '); $this->_query = rtrim($this->_query, ' AND ');
} }
// Did the user call the "groupBy" method?
if (!empty($this->_groupBy)) {
$this->_query .= " GROUP BY ";
foreach ($this->_groupBy as $key => $value) {
// prepares the reset of the SQL query.
$this->_query .= $value . ", ";
}
$this->_query = rtrim($this->_query, ', ') . " ";
}
// Did the user call the "orderBy" method? // Did the user call the "orderBy" method?
if (!empty ($this->_orderBy)) { if (!empty ($this->_orderBy)) {
$this->_query .= " order by "; $this->_query .= " ORDER BY ";
foreach ($this->_orderBy as $prop => $value) { foreach ($this->_orderBy as $prop => $value) {
// prepares the reset of the SQL query. // prepares the reset of the SQL query.
$this->_query .= $prop . " " . $value . ", "; $this->_query .= $prop . " " . $value . ", ";
......
...@@ -125,3 +125,9 @@ $db->orderBy("id","asc"); ...@@ -125,3 +125,9 @@ $db->orderBy("id","asc");
$db->orderBy("name","Desc"); $db->orderBy("name","Desc");
$results = $db->get('tableName'); $results = $db->get('tableName');
``` ```
### Grouping method
```php
$db->groupBy("name");
$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