summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.markdown8
-rw-r--r--idiorm.php23
-rw-r--r--test/test_queries.php8
3 files changed, 39 insertions, 0 deletions
diff --git a/README.markdown b/README.markdown
index 305c745..64b5595 100644
--- a/README.markdown
+++ b/README.markdown
@@ -182,6 +182,14 @@ Two methods are provided to add `ORDER BY` clauses to your query. These are `ord
$people = ORM::for_table('person')->order_by_asc('gender')->order_by_desc('name')->find_many();
+#### Grouping ####
+
+*Note that this method **does not** escape it query parameter and so this should **not** by passed directly from user input.*
+
+To add a column to GROUP BY to your query, call the `group_by` method, passing in the column name.
+
+ $poeple = ORM::for_table('person')->where('gender', 'female')->group_by('name')->find_many();
+
#### Result columns ####
By default, all columns in the `SELECT` statement are returned from your query. That is, calling:
diff --git a/idiorm.php b/idiorm.php
index e8713d6..ecace53 100644
--- a/idiorm.php
+++ b/idiorm.php
@@ -124,6 +124,9 @@
// ORDER BY
protected $_order_by = array();
+ // GROUP BY
+ protected $_group_by = array();
+
// The data for a hydrated instance of the class
protected $_data = array();
@@ -746,6 +749,15 @@
}
/**
+ * Add a column to the list of columns to GROUP BY
+ */
+ public function group_by($column_name) {
+ $column_name = $this->_quote_identifier($column_name);
+ $this->_group_by[] = $column_name;
+ return $this;
+ }
+
+ /**
* Build a SELECT statement based on the clauses that have
* been passed to this instance by chaining method calls.
*/
@@ -763,6 +775,7 @@
$this->_build_select_start(),
$this->_build_join(),
$this->_build_where(),
+ $this->_build_group_by(),
$this->_build_order_by(),
$this->_build_limit(),
$this->_build_offset(),
@@ -817,6 +830,16 @@
}
/**
+ * Build GROUP BY
+ */
+ protected function _build_group_by() {
+ if (count($this->_group_by) === 0) {
+ return '';
+ }
+ return "GROUP BY " . join(", ", $this->_group_by);
+ }
+
+ /**
* Build ORDER BY
*/
protected function _build_order_by() {
diff --git a/test/test_queries.php b/test/test_queries.php
index 2dca817..e227e27 100644
--- a/test/test_queries.php
+++ b/test/test_queries.php
@@ -84,6 +84,14 @@
$expected = "SELECT * FROM `widget` ORDER BY `name` ASC, `age` DESC LIMIT 1";
Tester::check_equal("Multiple ORDER BY", $expected);
+ ORM::for_table('widget')->group_by('name')->find_many();
+ $expected = "SELECT * FROM `widget` GROUP BY `name`";
+ Tester::check_equal("GROUP BY", $expected);
+
+ ORM::for_table('widget')->group_by('name')->group_by('age')->find_many();
+ $expected = "SELECT * FROM `widget` GROUP BY `name`, `age`";
+ Tester::check_equal("Multiple GROUP BY", $expected);
+
ORM::for_table('widget')->where('name', 'Fred')->limit(5)->offset(5)->order_by_asc('name')->find_many();
$expected = "SELECT * FROM `widget` WHERE `name` = 'Fred' ORDER BY `name` ASC LIMIT 5 OFFSET 5";
Tester::check_equal("Complex query", $expected);