@@ -57,11 +57,17 @@ class MysqliDb
5757 */
5858 protected $ _join = array ();
5959 /**
60- * An array that holds where conditions 'fieldname' => 'value'
60+ * An array that holds where conditions
6161 *
6262 * @var array
6363 */
6464 protected $ _where = array ();
65+ /**
66+ * An array that holds having conditions
67+ *
68+ * @var array
69+ */
70+ protected $ _having = array ();
6571 /**
6672 * Dynamic type list for order by condition value
6773 */
@@ -259,6 +265,7 @@ protected function reset()
259265 $ this ->trace [] = array ($ this ->_lastQuery , (microtime (true ) - $ this ->traceStartQ ) , $ this ->_traceGetCaller ());
260266
261267 $ this ->_where = array ();
268+ $ this ->_having = array ();
262269 $ this ->_join = array ();
263270 $ this ->_orderBy = array ();
264271 $ this ->_groupBy = array ();
@@ -681,6 +688,45 @@ public function orWhere($whereProp, $whereValue = 'DBNULL', $operator = '=')
681688 {
682689 return $ this ->where ($ whereProp , $ whereValue , $ operator , 'OR ' );
683690 }
691+
692+ /*
693+ * This method allows you to specify multiple (method chaining optional) AND HAVING statements for SQL queries.
694+ *
695+ * @uses $MySqliDb->having('SUM(tags) > 10')
696+ *
697+ * @param string $havingProp The name of the database field.
698+ * @param mixed $havingValue The value of the database field.
699+ *
700+ * @return MysqliDb
701+ */
702+ public function having ($ havingProp , $ havingValue = null , $ operator = null )
703+ {
704+ if ($ operator )
705+ $ havingValue = Array ($ operator => $ havingValue );
706+
707+ $ this ->_having [] = Array ("AND " , $ havingValue , $ havingProp );
708+ return $ this ;
709+ }
710+
711+ /**
712+ * This method allows you to specify multiple (method chaining optional) OR HAVING statements for SQL queries.
713+ *
714+ * @uses $MySqliDb->orHaving('SUM(tags) > 10')
715+ *
716+ * @param string $havingProp The name of the database field.
717+ * @param mixed $havingValue The value of the database field.
718+ *
719+ * @return MysqliDb
720+ */
721+ public function orHaving ($ havingProp , $ havingValue = null , $ operator = null )
722+ {
723+ if ($ operator )
724+ $ havingValue = Array ($ operator => $ havingValue );
725+
726+ $ this ->_having [] = Array ("OR " , $ havingValue , $ havingProp );
727+ return $ this ;
728+ }
729+
684730 /**
685731 * This method allows you to concatenate joins for the final SQL statement.
686732 *
@@ -911,8 +957,9 @@ protected function _buildQuery($numRows = null, $tableData = null)
911957 {
912958 $ this ->_buildJoin ();
913959 $ this ->_buildInsertQuery ($ tableData );
914- $ this ->_buildWhere ( );
960+ $ this ->_buildCondition ( ' WHERE ' , $ this -> _where );
915961 $ this ->_buildGroupBy ();
962+ $ this ->_buildCondition ('HAVING ' , $ this ->_having );
916963 $ this ->_buildOrderBy ();
917964 $ this ->_buildLimit ($ numRows );
918965 $ this ->_buildOnDuplicate ($ tableData );
@@ -1140,14 +1187,14 @@ protected function _buildInsertQuery ($tableData) {
11401187 /**
11411188 * Abstraction method that will build the part of the WHERE conditions
11421189 */
1143- protected function _buildWhere ( ) {
1144- if (empty ($ this -> _where ))
1190+ protected function _buildCondition ( $ operator , & $ conditions ) {
1191+ if (empty ($ conditions ))
11451192 return ;
11461193
11471194 //Prepare the where portion of the query
1148- $ this ->_query .= ' WHERE ' ;
1195+ $ this ->_query .= ' ' . $ operator ;
11491196
1150- foreach ($ this -> _where as $ cond ) {
1197+ foreach ($ conditions as $ cond ) {
11511198 list ($ concat , $ varName , $ operator , $ val ) = $ cond ;
11521199 $ this ->_query .= " " . $ concat ." " . $ varName ;
11531200
0 commit comments