@@ -57,11 +57,17 @@ class MysqliDb
57
57
*/
58
58
protected $ _join = array ();
59
59
/**
60
- * An array that holds where conditions 'fieldname' => 'value'
60
+ * An array that holds where conditions
61
61
*
62
62
* @var array
63
63
*/
64
64
protected $ _where = array ();
65
+ /**
66
+ * An array that holds having conditions
67
+ *
68
+ * @var array
69
+ */
70
+ protected $ _having = array ();
65
71
/**
66
72
* Dynamic type list for order by condition value
67
73
*/
@@ -259,6 +265,7 @@ protected function reset()
259
265
$ this ->trace [] = array ($ this ->_lastQuery , (microtime (true ) - $ this ->traceStartQ ) , $ this ->_traceGetCaller ());
260
266
261
267
$ this ->_where = array ();
268
+ $ this ->_having = array ();
262
269
$ this ->_join = array ();
263
270
$ this ->_orderBy = array ();
264
271
$ this ->_groupBy = array ();
@@ -681,6 +688,45 @@ public function orWhere($whereProp, $whereValue = 'DBNULL', $operator = '=')
681
688
{
682
689
return $ this ->where ($ whereProp , $ whereValue , $ operator , 'OR ' );
683
690
}
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
+
684
730
/**
685
731
* This method allows you to concatenate joins for the final SQL statement.
686
732
*
@@ -911,8 +957,9 @@ protected function _buildQuery($numRows = null, $tableData = null)
911
957
{
912
958
$ this ->_buildJoin ();
913
959
$ this ->_buildInsertQuery ($ tableData );
914
- $ this ->_buildWhere ( );
960
+ $ this ->_buildCondition ( ' WHERE ' , $ this -> _where );
915
961
$ this ->_buildGroupBy ();
962
+ $ this ->_buildCondition ('HAVING ' , $ this ->_having );
916
963
$ this ->_buildOrderBy ();
917
964
$ this ->_buildLimit ($ numRows );
918
965
$ this ->_buildOnDuplicate ($ tableData );
@@ -1140,14 +1187,14 @@ protected function _buildInsertQuery ($tableData) {
1140
1187
/**
1141
1188
* Abstraction method that will build the part of the WHERE conditions
1142
1189
*/
1143
- protected function _buildWhere ( ) {
1144
- if (empty ($ this -> _where ))
1190
+ protected function _buildCondition ( $ operator , & $ conditions ) {
1191
+ if (empty ($ conditions ))
1145
1192
return ;
1146
1193
1147
1194
//Prepare the where portion of the query
1148
- $ this ->_query .= ' WHERE ' ;
1195
+ $ this ->_query .= ' ' . $ operator ;
1149
1196
1150
- foreach ($ this -> _where as $ cond ) {
1197
+ foreach ($ conditions as $ cond ) {
1151
1198
list ($ concat , $ varName , $ operator , $ val ) = $ cond ;
1152
1199
$ this ->_query .= " " . $ concat ." " . $ varName ;
1153
1200
0 commit comments