|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rougin\Wildfire; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Result Trait |
|
7
|
|
|
* |
|
8
|
|
|
* Contains functions for retrieving data. |
|
9
|
|
|
* |
|
10
|
|
|
* @package Wildfire |
|
11
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
trait ResultTrait |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $tables = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Lists all data in dropdown format. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $description |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
3 |
|
public function asDropdown($description = 'description') |
|
27
|
|
|
{ |
|
28
|
3 |
|
$data = []; |
|
29
|
3 |
|
$id = $this->describe->getPrimaryKey($this->table); |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
3 |
|
$result = $this->query->result(); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
3 |
|
foreach ($result as $row) { |
|
34
|
3 |
|
$data[$row->$id] = ucwords($row->$description); |
|
35
|
3 |
|
} |
|
36
|
|
|
|
|
37
|
3 |
|
return $data; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Return the result |
|
42
|
|
|
* |
|
43
|
|
|
* @return object |
|
44
|
|
|
*/ |
|
45
|
9 |
|
public function result() |
|
46
|
|
|
{ |
|
47
|
9 |
|
$data = $this->getQueryResult(); |
|
48
|
9 |
|
$result = []; |
|
49
|
|
|
|
|
50
|
9 |
|
if (empty($this->table)) { |
|
|
|
|
|
|
51
|
3 |
|
$this->get(); |
|
|
|
|
|
|
52
|
3 |
|
} |
|
53
|
|
|
|
|
54
|
9 |
|
foreach ($data as $row) |
|
55
|
|
|
{ |
|
56
|
9 |
|
$object = $this->createObject($this->table, $row); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
9 |
|
array_push($result, $object); |
|
59
|
9 |
|
} |
|
60
|
|
|
|
|
61
|
9 |
|
return $result; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Create an object from the specified data |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $table |
|
68
|
|
|
* @param object $row |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
12 |
|
protected function createObject($table, $row) |
|
72
|
|
|
{ |
|
73
|
12 |
|
$newTable = ucfirst(Inflector::singular($table)); |
|
74
|
12 |
|
$model = new $newTable; |
|
75
|
|
|
|
|
76
|
12 |
|
if ( ! array_key_exists($table, $this->tables)) { |
|
77
|
12 |
|
$tableInfo = $this->describe->getTable($table); |
|
78
|
|
|
|
|
79
|
12 |
|
$this->tables[$table] = $tableInfo; |
|
80
|
12 |
|
} else { |
|
81
|
9 |
|
$tableInfo = $this->tables[$table]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
12 |
|
foreach ($row as $key => $value) { |
|
85
|
12 |
|
foreach ($tableInfo as $column) { |
|
86
|
12 |
|
if ($column->getField() != $key) { |
|
87
|
12 |
|
continue; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
12 |
|
$model->$key = $value; |
|
91
|
12 |
|
} |
|
92
|
12 |
|
} |
|
93
|
|
|
|
|
94
|
12 |
|
foreach ($row as $key => $value) { |
|
95
|
12 |
|
foreach ($tableInfo as $column) { |
|
96
|
12 |
|
if ($column->getField() != $key || ! $column->isForeignKey()) { |
|
97
|
12 |
|
continue; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
12 |
|
$foreignColumn = $column->getReferencedField(); |
|
101
|
12 |
|
$foreignTable = $column->getReferencedTable(); |
|
102
|
|
|
|
|
103
|
12 |
|
$delimiters = [ $foreignColumn => $value ]; |
|
104
|
12 |
|
$foreignData = $this->find($foreignTable, $delimiters); |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
12 |
|
$newColumn = Inflector::singular($foreignTable); |
|
107
|
|
|
|
|
108
|
12 |
|
$model->$newColumn = $foreignData; |
|
109
|
12 |
|
} |
|
110
|
12 |
|
} |
|
111
|
|
|
|
|
112
|
12 |
|
return $model; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Gets the data result from the specified query. |
|
117
|
|
|
* |
|
118
|
|
|
* @return array|object |
|
119
|
|
|
*/ |
|
120
|
9 |
|
protected function getQueryResult() |
|
121
|
|
|
{ |
|
122
|
9 |
|
$result = $this->query; |
|
123
|
|
|
|
|
124
|
9 |
|
if (method_exists($this->query, 'result')) { |
|
125
|
9 |
|
$result = $this->query->result(); |
|
126
|
9 |
|
} |
|
127
|
|
|
|
|
128
|
9 |
|
return $result; |
|
129
|
|
|
} |
|
130
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: