|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rougin\Wildfire\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Rougin\Describe\Column; |
|
6
|
|
|
|
|
7
|
|
|
use Rougin\Wildfire\CodeigniterModel; |
|
8
|
|
|
use Rougin\Wildfire\Helpers\ModelHelper; |
|
9
|
|
|
use Rougin\Wildfire\Helpers\TableHelper; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Object Trait |
|
13
|
|
|
* |
|
14
|
|
|
* @package Wildfire |
|
15
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
|
16
|
|
|
* |
|
17
|
|
|
* @property \Rougin\Describe\Describe $describe |
|
18
|
|
|
* @property string $table |
|
19
|
|
|
*/ |
|
20
|
|
|
trait ObjectTrait |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Creates an object from the specified table and row. |
|
24
|
|
|
* NOTE: To be removed in v1.0.0. Use $this->make instead. |
|
25
|
|
|
* |
|
26
|
|
|
* @param string|\Rougin\Wildfire\CodeigniterModel $table |
|
27
|
|
|
* @param object $row |
|
28
|
|
|
* @return array |
|
29
|
|
|
*/ |
|
30
|
36 |
|
protected function createObject($table, $row) |
|
31
|
|
|
{ |
|
32
|
36 |
|
return $this->make($table, $row); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Creates an object from the specified table and row. |
|
37
|
|
|
* |
|
38
|
|
|
* @param \Rougin\Wildfire\CodeigniterModel\string $table |
|
39
|
|
|
* @param object $row |
|
40
|
|
|
* @return array |
|
41
|
|
|
*/ |
|
42
|
36 |
|
protected function make($table, $row) |
|
43
|
|
|
{ |
|
44
|
36 |
|
list($table, $model) = ModelHelper::make($table); |
|
45
|
|
|
|
|
46
|
36 |
|
$properties = array('belongs_to' => array()); |
|
47
|
|
|
|
|
48
|
36 |
|
$properties['hidden'] = array(); |
|
49
|
|
|
|
|
50
|
36 |
|
$properties['columns'] = array(); |
|
51
|
|
|
|
|
52
|
36 |
|
if ($model instanceof CodeigniterModel === true) { |
|
53
|
36 |
|
$properties = $model->properties(); |
|
54
|
|
|
|
|
55
|
36 |
|
$properties = $model->relationships($properties); |
|
56
|
36 |
|
} |
|
57
|
|
|
|
|
58
|
36 |
|
$columns = $this->describe->columns($table); |
|
59
|
|
|
|
|
60
|
36 |
|
$this->setModelFields($model, $row, $properties, $columns); |
|
61
|
|
|
|
|
62
|
36 |
|
return $model; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Finds the row from the specified ID or with the list of delimiters from |
|
67
|
|
|
* the specified table. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $table |
|
70
|
|
|
* @param array|integer $delimiters |
|
71
|
|
|
* @return object|boolean |
|
72
|
|
|
*/ |
|
73
|
|
|
abstract protected function find($table, $delimiters = []); |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Checks if the field is not a column or intended to be hidden. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $key |
|
79
|
|
|
* @param array $properties |
|
80
|
|
|
* @return boolean |
|
81
|
|
|
*/ |
|
82
|
36 |
|
protected function notColumnOrIsHidden($key, array $properties) |
|
83
|
|
|
{ |
|
84
|
36 |
|
$isColumn = in_array($key, $properties['columns']); |
|
85
|
36 |
|
$isHidden = in_array($key, $properties['hidden']); |
|
86
|
|
|
|
|
87
|
36 |
|
return (! empty($properties['columns']) && ! $isColumn) || $isHidden; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Sets the foreign field of the column, if any. |
|
92
|
|
|
* |
|
93
|
|
|
* @param \CI_Model $model |
|
94
|
|
|
* @param \Rougin\Describe\Column $column |
|
95
|
|
|
* @param array $properties |
|
96
|
|
|
* @return void |
|
97
|
|
|
*/ |
|
98
|
36 |
|
protected function setForeignField(\CI_Model $model, Column $column, array $properties) |
|
99
|
|
|
{ |
|
100
|
36 |
|
$columnName = $column->getField(); |
|
101
|
|
|
|
|
102
|
36 |
|
$foreignColumn = $column->getReferencedField(); |
|
103
|
36 |
|
$foreignTable = $column->getReferencedTable(); |
|
104
|
|
|
|
|
105
|
36 |
|
if (in_array($foreignTable, $properties['belongs_to'])) { |
|
106
|
6 |
|
$delimiters = [ $foreignColumn => $model->$columnName ]; |
|
107
|
6 |
|
$foreign = $this->find($foreignTable, $delimiters); |
|
108
|
|
|
|
|
109
|
6 |
|
if (is_object($foreign)) { |
|
110
|
6 |
|
$tableName = TableHelper::getNameFromModel($foreign); |
|
111
|
|
|
|
|
112
|
6 |
|
$model->$tableName = $foreign; |
|
113
|
6 |
|
} |
|
114
|
6 |
|
} |
|
115
|
36 |
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Sets the model values based on the result row. |
|
119
|
|
|
* |
|
120
|
|
|
* @param \CI_Model &$model |
|
121
|
|
|
* @param object $row |
|
122
|
|
|
* @param array $properties |
|
123
|
|
|
* @param array $tableInformation |
|
124
|
|
|
* @return void |
|
125
|
|
|
*/ |
|
126
|
36 |
|
protected function setModelFields(&$model, $row, array $properties, $tableInformation) |
|
127
|
|
|
{ |
|
128
|
36 |
|
foreach ($tableInformation as $column) { |
|
129
|
36 |
|
$key = $column->getField(); |
|
130
|
|
|
|
|
131
|
36 |
|
if ($this->notColumnOrIsHidden($key, $properties)) { |
|
132
|
36 |
|
continue; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
36 |
|
$model->$key = $row->$key; |
|
136
|
|
|
|
|
137
|
36 |
|
$this->setForeignField($model, $column, $properties); |
|
138
|
36 |
|
} |
|
139
|
36 |
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: