Skip to content

Commit 76c2f53

Browse files
authored
Merge pull request #5 from iazaran/search_method
Search method
2 parents 7ad1e66 + de0cc79 commit 76c2f53

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
- **Get information about the tables and columns**
1010
The `information` method can generate information about the tables and columns. So frontend can see the columns and their types.
1111
- **Generate simple CRUD automatically**
12-
The `create`, `read`, `update` and `delete` methods can accept the different parameters that determine the target table(s) and column(s) to apply CRUD. You can add your custom method to do more process on specific requests. (WIP: adding action to the existing method to handle specific requests and processes)
12+
The `create`, `read`, `update` and `delete` methods can accept the different parameters that determine the target table(s) and column(s) to apply CRUD. You can add your custom method to do more process on specific requests.
1313
- **Can be used by a single endpoint in API**
1414
This package doesn't serve API features, but you can set an endpoint to accept those parameters and do CRUD features like GraphQL.
1515

@@ -35,6 +35,8 @@ $generatrixCRUD::create(['tt_countries' => ['countryCode' => ['US', 'GB'], 'coun
3535
$generatrixCRUD::update(237, ['tt_countries' => ['countryCode' => 'ES', 'countryName' => 'Spain']]);
3636
// Delete specific row
3737
$generatrixCRUD::delete(237, ['tt_countries']);
38+
// Search for multiple columns (AND, OR, XOR, ...) of target table (=, LIKE, NOT, ...) and list them ('AND' will be considered for joining conditions of conditions) You can add relationships like read method
39+
$generatrixCRUD::search(['OR' => ['=' => ['cityName' => 'dubai'], 'LIKE' => ['cityName' => 'old']]], ['tt_cities' => ['cityName']]);
3840
```
3941

4042
------------

samples.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public static function groupByFirstColumn(...$responses): array
4949

5050
// Delete specific row
5151
var_dump($generatrixCRUD::delete(237, ['tt_countries']));
52+
53+
// Search for multiple columns (AND, OR, XOR, ...) of target table (=, LIKE, NOT, ...) and list them ('AND' will be considered for joining conditions of conditions) You can add relationships like read method
54+
var_dump($generatrixCRUD::search(['OR' => ['=' => ['cityName' => 'dubai'], 'LIKE' => ['cityName' => 'old']]], ['tt_cities' => ['cityName']]));
5255
} catch (Exception $e) {
5356
var_dump($e->getMessage());
5457
}

src/GeneratrixCRUD.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,95 @@ public static function delete(int $id, array $table, array $callback = []): mysq
238238
default => $result,
239239
};
240240
}
241+
242+
/**
243+
* Handle SEARCH request
244+
*
245+
* @param array $search
246+
* @param array $table
247+
* @param array $relationships
248+
* @param string $relationshipDirection
249+
* @param array $callback
250+
* @return array|false|string
251+
*/
252+
public static function search(array $search, array $table, array $relationships = [], string $relationshipDirection = 'LEFT', array $callback = []): bool|array|string
253+
{
254+
$tableName = array_key_first($table);
255+
256+
$columnNames = ["$tableName.*"];
257+
if ($table[$tableName][0] != '*') {
258+
$columnNames = [];
259+
foreach ($table[$tableName] as $column) {
260+
$columnNames[] = "$tableName.$column";
261+
}
262+
}
263+
264+
$columnNames = implode(', ', $columnNames);
265+
$sql = "SELECT $columnNames";
266+
267+
$relatedSql = [];
268+
if (count($relationships) > 0) {
269+
foreach ($relationships as $key => $value) {
270+
$relatedSql[$key] = " JOIN $key";
271+
272+
$columnNames = ["$key.*"];
273+
if ($value[0] != '*') {
274+
$columnNames = [];
275+
foreach ($value as $column) {
276+
$columnNames[] = "$key.$column";
277+
}
278+
}
279+
280+
$columnNames = implode(', ', $columnNames);
281+
}
282+
283+
$sql .= ", $columnNames";
284+
}
285+
286+
$sql .= " FROM $tableName";
287+
288+
if (count($relatedSql) > 0) {
289+
foreach ($relatedSql as $key => $value) {
290+
if ($relationshipDirection == 'LEFT') {
291+
$sql .= $value . " ON $tableName." . self::$generatrixDB->dbRelationships[$key]['REFERENCED_COLUMN_NAME'] . " = $key." . self::$generatrixDB->dbRelationships[$key]['COLUMN_NAME'];
292+
} else {
293+
$sql .= $value . " ON $tableName." . self::$generatrixDB->dbRelationships[$tableName]['COLUMN_NAME'] . " = $key." . self::$generatrixDB->dbRelationships[$tableName]['REFERENCED_COLUMN_NAME'];
294+
}
295+
}
296+
}
297+
298+
$sql .= ' WHERE ';
299+
$searchesArray = [];
300+
foreach ($search as $key => $value) {
301+
$searchArray = [];
302+
$conditionsOperator = $key;
303+
foreach ($value as $k => $v) {
304+
if ($k == 'LIKE') {
305+
$searchArray[] = "$tableName." . array_key_first($v) . " $k '%" . $v[array_key_first($v)] . "%'";
306+
} else {
307+
$searchArray[] = "$tableName." . array_key_first($v) . " $k '" . $v[array_key_first($v)] . "'";
308+
}
309+
}
310+
$searchesArray[] = '(' . implode(" $conditionsOperator ", $searchArray) . ')';
311+
}
312+
$sql .= implode(' AND ', $searchesArray);
313+
314+
$result = self::$generatrixDB->dbConnection->query($sql);
315+
316+
$response = [];
317+
if ($result->num_rows > 0) {
318+
while ($row = $result->fetch_assoc()) {
319+
$response[] = $row;
320+
}
321+
}
322+
323+
if (count($callback) == 2 && is_callable($callback)) {
324+
$response = call_user_func_array($callback, $response);
325+
}
326+
327+
return match (self::$responseType) {
328+
'JSON' => json_encode($response),
329+
default => $response,
330+
};
331+
}
241332
}

0 commit comments

Comments
 (0)