-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.php
More file actions
81 lines (67 loc) · 2 KB
/
Model.php
File metadata and controls
81 lines (67 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
// Import additionnal class into the global namespace
use \LaswitchTech\Core\Base\BaseModel;
class ProcessModel extends BaseModel {
/**
* Constructor
*/
public function __construct()
{
// Call the parent constructor
parent::__construct();
// Initialize the Model
$this->init('processes');
}
/**
* Process a record
*
* @param array $record
* @return array
*/
protected function process(array $record): array
{
// Call the parent constructor
$record = parent::process($record);
// Decode the process
if(!is_array($record['process'])){
$record['process'] = json_decode($record['process'] ?? "[]", true);
}
// Return the processed record
return $record;
}
/**
* Retrieve a single record by target Table
*
* @param string $table
* @param string|null $category
* @return array
*/
public function fetchByTable(string $table, ?string $category = null): array
{
// Create the Query
$Query = $this->Database->query()
->table($this->table)
->select('*')
->join('owner', 'users', 'username')
->order('id', 'DESC')
->filter()
->where('id', 9999, '<>')
->filter()
->where('targetTable', $table)
->limit(1);
// Check if a category is provided
if($category && !empty($category)){
// Add the category condition
$Query->where('category', $category);
}
// Retrieve the record
$records = $Query->fetch();
// Loop through the records to process them
foreach($records as $key => $record){
// Overwrite the record with the processed one
$records[$key] = $this->process($record);
}
// Return the record or an empty array if not found
return $records[array_key_first($records)] ?? [];
}
}