Skip to content

Commit e4746f8

Browse files
committed
Merge pull request #349 from avbdr/master
fixes
2 parents 23f6f77 + d4120c6 commit e4746f8

File tree

2 files changed

+71
-8
lines changed

2 files changed

+71
-8
lines changed

MysqliDb.php

+53-6
Original file line numberDiff line numberDiff line change
@@ -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

readme.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,8 @@ $resutls = $db->rawQuery ($q, $params);
286286
print_r ($results); // contains Array of returned rows
287287
```
288288

289-
### Where Method
290-
This method allows you to specify where parameters of the query.
289+
### Where / Having Methods
290+
`where()`, `orWhere()`, `having()` and `orHaving()` methods allows you to specify where and having conditions of the query. All conditions supported by where() are supported by having() as well.
291291

292292
WARNING: In order to use column to column comparisons only raw where conditions should be used as column name or functions cant be passed as a bind variable.
293293

@@ -299,6 +299,14 @@ $results = $db->get ('users');
299299
// Gives: SELECT * FROM users WHERE id=1 AND login='admin';
300300
```
301301

302+
```php
303+
$db->where ('id', 1);
304+
$db->having ('login', 'admin');
305+
$results = $db->get ('users');
306+
// Gives: SELECT * FROM users WHERE id=1 HAVING login='admin';
307+
```
308+
309+
302310
Regular == operator with column to column comparison:
303311
```php
304312
// WRONG
@@ -342,6 +350,14 @@ $results = $db->get ('users');
342350
// Gives: SELECT * FROM users WHERE firstName='John' OR firstName='peter'
343351
```
344352

353+
```php
354+
$db->where ('firstName', 'John');
355+
$db->orWhere ('firstName', 'Peter');
356+
$results = $db->get ('users');
357+
// Gives: SELECT * FROM users WHERE firstName='John' OR firstName='peter'
358+
```
359+
360+
345361
NULL comparison:
346362
```php
347363
$db->where ("lastName", NULL, '<=>');

0 commit comments

Comments
 (0)