Commit c199dc9d authored by Alexander Butenko's avatar Alexander Butenko

Added advanced where options

parent 548e5ac1
...@@ -331,11 +331,37 @@ class MysqliDb ...@@ -331,11 +331,37 @@ class MysqliDb
//Prepair the where portion of the query //Prepair the where portion of the query
$this->_query .= ' WHERE '; $this->_query .= ' WHERE ';
foreach ($this->_where as $column => $value) { foreach ($this->_where as $column => $value) {
$comparison = ' = ? ';
if( is_array( $value ) ) {
// if the value is an array, then this isn't a basic = comparison
$key = key( $value );
$val = $value[$key];
switch( strtolower($key) ) {
case 'in':
$comparison = ' IN (';
foreach($val as $v){
$comparison .= ' ?,';
$this->_whereTypeList .= $this->_determineType( $v );
}
$comparison = rtrim($comparison, ',').' ) ';
break;
case 'between':
$comparison = ' BETWEEN ? AND ? ';
$this->_whereTypeList .= $this->_determineType( $val[0] );
$this->_whereTypeList .= $this->_determineType( $val[1] );
break;
default:
// We are using a comparison operator with only one parameter after it
$comparison = ' '.$key.' ? ';
// Determines what data type the where column is, for binding purposes.
$this->_whereTypeList .= $this->_determineType( $val );
}
} else {
// Determines what data type the where column is, for binding purposes. // Determines what data type the where column is, for binding purposes.
$this->_whereTypeList .= $this->_determineType($value); $this->_whereTypeList .= $this->_determineType($value);
}
// Prepares the reset of the SQL query. // Prepares the reset of the SQL query.
$this->_query .= ($column . ' = ? AND '); $this->_query .= ($column.$comparison.' AND ');
} }
$this->_query = rtrim($this->_query, ' AND '); $this->_query = rtrim($this->_query, ' AND ');
} }
...@@ -387,7 +413,22 @@ class MysqliDb ...@@ -387,7 +413,22 @@ class MysqliDb
if ($this->_where) { if ($this->_where) {
$this->_bindParams[0] .= $this->_whereTypeList; $this->_bindParams[0] .= $this->_whereTypeList;
foreach ($this->_where as $prop => $val) { foreach ($this->_where as $prop => $val) {
array_push($this->_bindParams, $this->_where[$prop]); if (!is_array ($val)) {
array_push ($this->_bindParams, $this->_where[$prop]);
continue;
}
// if val is an array, this is not a basic = comparison operator
$key = key($val);
$vals = $val[$key];
if (is_array($vals)) {
// if vals is an array, this comparison operator takes more than one parameter
foreach ($vals as $k => $v) {
array_push($this->_bindParams, $this->_where[$prop][$key][$k]);
}
} else {
// otherwise this comparison operator takes only one parameter
array_push ($this->_bindParams, $this->_where[$prop][$key]);
}
} }
} }
} }
......
...@@ -73,6 +73,7 @@ print_r($results); // contains array of returned rows ...@@ -73,6 +73,7 @@ print_r($results); // contains array of returned rows
### Where Method ### Where Method
This method allows you to specify the parameters of the query. This method allows you to specify the parameters of the query.
Regular == operator:
```php ```php
$db->where('id', int); $db->where('id', int);
$db->where('title', string); $db->where('title', string);
...@@ -80,6 +81,27 @@ $results = $db->get('tableName'); ...@@ -80,6 +81,27 @@ $results = $db->get('tableName');
print_r($results); // contains array of returned rows print_r($results); // contains array of returned rows
``` ```
Custom Operators:
```php
$db->where( 'id', array( '>=' => 50 ) );
$results = $db->get('tableName');
// Gives: SELECT * FROM tableName WHERE id >= ?
```
BETWEEN:
```php
$db->where( 'id', array( 'between' => array(4, 20) ) );
$results = $db->get('tableName');
// Gives: SELECT * FROM tableName WHERE id BETWEEN ? AND ?
```
IN:
``php
$db->where( 'id', array( 'in' => array(1, 5, 27, -1, 'd') ) );
$results = $db->get('tableName');
// Gives: SELECT * FROM tableName WHERE id IN ( ?, ?, ?, ?, ? )
```
Optionally you can use method chaining to call where multiple times without referencing your object over an over: Optionally you can use method chaining to call where multiple times without referencing your object over an over:
```php ```php
......
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