Browse Source

Added multi-filter documentation and option for AND and OR using satisfy.

Maurits van der Schee 10 years ago
parent
commit
b33d932938
2 changed files with 17 additions and 6 deletions
  1. 13
    3
      README.md
  2. 4
    3
      api.php

+ 13
- 3
README.md View File

@@ -32,7 +32,7 @@ This is a single file application! Upload "api.php" somewhere and enjoy!
32 32
   - Permission system for databases and tables
33 33
   - JSONP support for cross-domain requests
34 34
   - Combined requests with support for multiple table names
35
-  - Pagination, sorting and search support
35
+  - Pagination, sorting, column selection and search support
36 36
   - Relation detection and filtering on foreign keys
37 37
   - Relation "transforms" for PHP and JavaScript
38 38
 
@@ -131,7 +131,7 @@ Search is implemented with the "filter" parameter. You need to specify the colum
131 131
   - ge: greater or equal (number is higher than or equal to value)
132 132
   - gt: greater than (number is higher than value)
133 133
   - in: in (number is in comma seperated list of values)
134
-
134
+  
135 135
 ```
136 136
 GET http://localhost/api.php/categories?filter=name,eq,Internet
137 137
 GET http://localhost/api.php/categories?filter=name,sw,Inter
@@ -139,10 +139,20 @@ GET http://localhost/api.php/categories?filter=id,le,1
139 139
 GET http://localhost/api.php/categories?filter=id,lt,2
140 140
 ```
141 141
 
142
+### List + Filter + Satisfy
143
+
144
+Multiple filters can be applied by using "filter[]" instead of "filter" as a parameter name. Then the parameter "satisfy" is used to indicate whether "all" (default) or "any" filter should be satisfied to lead to a match:
145
+
146
+```
147
+GET http://localhost/api.php/categories?filter[]=id,eq,1&filter[]=id,eq,3&satisfy=any
148
+GET http://localhost/api.php/categories?filter[]=id,ge,1&filter[]=id,le,3&satisfy=all
149
+GET http://localhost/api.php/categories?filter[]=id,ge,1&filter[]=id,le,3
150
+```
151
+
142 152
 Output:
143 153
 
144 154
 ```
145
-{"categories":{"columns":["id","name"],"records":[["1","Internet"]]}}
155
+{"categories":{"columns":["id","name"],"records":[["1","Internet"],["3","Web development"]]}}
146 156
 ```
147 157
 
148 158
 ### List + Column selection

+ 4
- 3
api.php View File

@@ -457,6 +457,7 @@ class REST_CRUD_API {
457 457
 		$callback  = $this->parseGetParameter($get, 'callback', 'a-zA-Z0-9\-_', false);
458 458
 		$page      = $this->parseGetParameter($get, 'page', '0-9,', false);
459 459
 		$filters   = $this->parseGetParameterArray($get, 'filter', false, false);
460
+		$satisfy   = $this->parseGetParameter($get, 'satisfy', 'a-z', 'all');
460 461
 		$columns   = $this->parseGetParameter($get, 'columns', 'a-zA-Z0-9\-_,', false);
461 462
 		$order     = $this->parseGetParameter($get, 'order', 'a-zA-Z0-9\-_*,', false);
462 463
 		$transform = $this->parseGetParameter($get, 'transform', '1', false);
@@ -476,7 +477,7 @@ class REST_CRUD_API {
476 477
 
477 478
 		list($collect,$select) = $this->findRelations($table,$database,$db);
478 479
 
479
-		return compact('action','database','table','key','callback','page','filters','columns','order','transform','db','object','input','collect','select');
480
+		return compact('action','database','table','key','callback','page','filters','satisfy','columns','order','transform','db','object','input','collect','select');
480 481
 	}
481 482
 
482 483
 	protected function listCommand($parameters) {
@@ -494,7 +495,7 @@ class REST_CRUD_API {
494 495
 			$params[] = $table;
495 496
 			foreach ($filters as $i=>$filter) {
496 497
 				if (is_array($filter)) {
497
-					$sql .= $i==0?' WHERE ':' AND ';
498
+					$sql .= $i==0?' WHERE ':$satisfy=='all'?' AND ':' OR ';
498 499
 					$sql .= '"!" ! ?';
499 500
 					$params[] = $filter[0];
500 501
 					$params[] = $filter[1];
@@ -518,7 +519,7 @@ class REST_CRUD_API {
518 519
 		$params[] = $table;
519 520
 		foreach ($filters as $i=>$filter) {
520 521
 			if (is_array($filter)) {
521
-				$sql .= $i==0?' WHERE ':' AND ';
522
+				$sql .= $i==0?' WHERE ':$satisfy=='all'?' AND ':' OR ';
522 523
 				$sql .= '"!" ! ?';
523 524
 				$params[] = $filter[0];
524 525
 				$params[] = $filter[1];

Loading…
Cancel
Save