Commit 6261af4f authored by Alexander Butenko's avatar Alexander Butenko

Added nicer where API to define operators

parent b6d5e9bf
...@@ -357,8 +357,11 @@ class MysqliDb ...@@ -357,8 +357,11 @@ class MysqliDb
* *
* @return MysqliDb * @return MysqliDb
*/ */
public function where($whereProp, $whereValue = null) public function where($whereProp, $whereValue = null, $operator = null)
{ {
if ($operator)
$whereValue = Array ($operator => $whereValue);
$this->_where[$whereProp] = Array ("AND", $whereValue); $this->_where[$whereProp] = Array ("AND", $whereValue);
return $this; return $this;
} }
...@@ -373,8 +376,11 @@ class MysqliDb ...@@ -373,8 +376,11 @@ class MysqliDb
* *
* @return MysqliDb * @return MysqliDb
*/ */
public function orWhere($whereProp, $whereValue = null) public function orWhere($whereProp, $whereValue = null, $operator = null)
{ {
if ($operator)
$whereValue = Array ($operator => $whereValue);
$this->_where[$whereProp] = Array ("OR", $whereValue); $this->_where[$whereProp] = Array ("OR", $whereValue);
return $this; return $this;
} }
......
...@@ -133,23 +133,23 @@ $results = $db->get('users'); ...@@ -133,23 +133,23 @@ $results = $db->get('users');
``` ```
```php ```php
$db->where('id', Array('>=' => 50)); $db->where('id', 50, ">=");
$results = $db->get('users'); $results = $db->get('users');
// Gives: SELECT * FROM users WHERE id >= 50; // Gives: SELECT * FROM users WHERE id >= 50;
``` ```
BETWEEN: BETWEEN:
```php ```php
$db->where('id', Array('between' => Array(4, 20) ) ); $db->where('id', Array(4, 20), 'between');
//$db->where('id', Array('not between' => Array(4, 20) ) ); //$db->where('id', Array(4, 20), 'not between');
$results = $db->get('users'); $results = $db->get('users');
// Gives: SELECT * FROM users WHERE id BETWEEN 4 AND 20 // Gives: SELECT * FROM users WHERE id BETWEEN 4 AND 20
``` ```
IN: IN:
```php ```php
$db->where('id', Array( 'in' => Array(1, 5, 27, -1, 'd') ) ); $db->where('id', Array(1, 5, 27, -1, 'd'), 'IN');
//$db->where('id', Array( 'not in' => Array(1, 5, 27, -1, 'd') ) ); //$db->where('id', Array(1, 5, 27, -1, 'd'), 'NOT IN');
$results = $db->get('users'); $results = $db->get('users');
// Gives: SELECT * FROM users WHERE id IN (1, 5, 27, -1, 'd'); // Gives: SELECT * FROM users WHERE id IN (1, 5, 27, -1, 'd');
``` ```
...@@ -164,7 +164,7 @@ $results = $db->get('users'); ...@@ -164,7 +164,7 @@ $results = $db->get('users');
NULL comparison: NULL comparison:
```php ```php
$db->where ("lastName", Array("<=>" => NULL)); $db->where ("lastName", NULL, '<=>');
$results = $db->get("users"); $results = $db->get("users");
// Gives: SELECT * FROM users where lastName <=> NULL // Gives: SELECT * FROM users where lastName <=> NULL
``` ```
......
...@@ -138,7 +138,7 @@ if ($db->count != 3) { ...@@ -138,7 +138,7 @@ if ($db->count != 3) {
//$users = $db->get("users"); //$users = $db->get("users");
//print_r ($users); //print_r ($users);
$db->where("firstname", Array("LIKE" => '%John%')); $db->where("firstname", '%John%', 'LIKE');
$users = $db->get("users"); $users = $db->get("users");
if ($db->count != 1) { if ($db->count != 1) {
echo "Invalid insert count in LIKE: ".$db->count; echo "Invalid insert count in LIKE: ".$db->count;
...@@ -172,14 +172,14 @@ if ($r['password'] != '546f98b24edfdc3b9bbe0d241bd8b29783f71b32') { ...@@ -172,14 +172,14 @@ if ($r['password'] != '546f98b24edfdc3b9bbe0d241bd8b29783f71b32') {
exit; exit;
} }
$db->where ("id", Array('in' => Array('1','2','3'))); $db->where ("id", Array('1','2','3'), 'IN');
$db->get("users"); $db->get("users");
if ($db->count != 3) { if ($db->count != 3) {
echo "Invalid users count on where() with in "; echo "Invalid users count on where() with in ";
exit; exit;
} }
$db->where ("id", Array('between' => Array('2','3'))); $db->where ("id", Array('2','3'), 'between');
$db->get("users"); $db->get("users");
if ($db->count != 2) { if ($db->count != 2) {
echo "Invalid users count on where() with between"; echo "Invalid users count on where() with between";
...@@ -194,7 +194,7 @@ if ($db->count != 2) { ...@@ -194,7 +194,7 @@ if ($db->count != 2) {
exit; exit;
} }
$db->where ("lastName", Array("<=>" => NULL)); $db->where ("lastName", NULL, '<=>');
$r = $db->get("users"); $r = $db->get("users");
if ($db->count != 1) { if ($db->count != 1) {
echo "Invalid users count on null where()"; echo "Invalid users count on null where()";
......
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