|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rougin\Wildfire\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use CI_Model; |
|
6
|
|
|
use Rougin\Describe\Column; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Object Trait |
|
10
|
|
|
* |
|
11
|
|
|
* @package Wildfire |
|
12
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
|
13
|
|
|
* |
|
14
|
|
|
* @property \Rougin\Describe\Describe $describe |
|
15
|
|
|
*/ |
|
16
|
|
|
trait ObjectTrait |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $tables = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Creates an object from the specified table and row. |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $table |
|
27
|
|
|
* @param object $row |
|
28
|
|
|
* @param boolean $isForeignKey |
|
29
|
|
|
* @return array |
|
30
|
|
|
*/ |
|
31
|
24 |
|
protected function createObject($table, $row, $isForeignKey = false) |
|
32
|
|
|
{ |
|
33
|
24 |
|
list($model, $newTable) = $this->getModel($table, $isForeignKey); |
|
34
|
|
|
|
|
35
|
24 |
|
if (! array_key_exists($newTable, $this->tables)) { |
|
36
|
21 |
|
$tableInfo = $this->describe->getTable($newTable); |
|
37
|
|
|
|
|
38
|
21 |
|
$this->tables[$newTable] = $tableInfo; |
|
39
|
21 |
|
} else { |
|
40
|
21 |
|
$tableInfo = $this->tables[$newTable]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
24 |
|
$columns = []; |
|
44
|
|
|
|
|
45
|
24 |
|
if (method_exists($model, 'getColumns')) { |
|
46
|
9 |
|
$columns = $model->getColumns(); |
|
47
|
24 |
|
} elseif (property_exists($model, 'columns')) { |
|
48
|
|
|
// NOTE: To be removed in v1.0.0 |
|
49
|
|
|
$columns = $model->columns; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
24 |
|
foreach ($tableInfo as $column) { |
|
53
|
24 |
|
$key = $column->getField(); |
|
54
|
|
|
|
|
55
|
24 |
|
if (! empty($columns) && ! in_array($key, $model->columns)) { |
|
56
|
|
|
continue; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
24 |
|
$model->$key = $row->$key; |
|
60
|
|
|
|
|
61
|
24 |
|
$this->setForeignField($model, $column); |
|
62
|
24 |
|
} |
|
63
|
|
|
|
|
64
|
24 |
|
return $model; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Finds the row from the specified ID or with the list of delimiters from |
|
69
|
|
|
* the specified table. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $table |
|
72
|
|
|
* @param array|integer $delimiters |
|
73
|
|
|
* @param boolean $isForeignKey |
|
74
|
|
|
* @return object|boolean |
|
75
|
|
|
*/ |
|
76
|
|
|
abstract protected function find($table, $delimiters = [], $isForeignKey = false); |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Sets the foreign field of the column, if any. |
|
80
|
|
|
* |
|
81
|
|
|
* @param \CI_Model $model |
|
82
|
|
|
* @param \Rougin\Describe\Column $column |
|
83
|
|
|
* @return void |
|
84
|
|
|
*/ |
|
85
|
24 |
|
protected function setForeignField(CI_Model $model, Column $column) |
|
86
|
|
|
{ |
|
87
|
24 |
|
if (! $column->isForeignKey()) { |
|
88
|
24 |
|
return; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
15 |
|
$key = $column->getField(); |
|
92
|
15 |
|
$foreignColumn = $column->getReferencedField(); |
|
93
|
15 |
|
$foreignTable = $column->getReferencedTable(); |
|
94
|
|
|
|
|
95
|
15 |
|
$delimiters = [ $foreignColumn => $model->$key ]; |
|
96
|
15 |
|
$foreignData = $this->find($foreignTable, $delimiters, true); |
|
97
|
15 |
|
$newColumn = $this->getTableName($foreignTable, true); |
|
98
|
|
|
|
|
99
|
15 |
|
$model->$newColumn = $foreignData; |
|
100
|
15 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Gets the model class of the said table. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string|null $table |
|
106
|
|
|
* @param boolean $isForeignKey |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
30 |
|
protected function getModel($table = null, $isForeignKey = false) |
|
110
|
|
|
{ |
|
111
|
30 |
|
if ($table == null && $this->table == null) { |
|
|
|
|
|
|
112
|
6 |
|
return [ null, '' ]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
30 |
|
$newTable = $this->getTableName($table, $isForeignKey); |
|
116
|
30 |
|
$model = new $newTable; |
|
117
|
|
|
|
|
118
|
30 |
|
if (method_exists($model, 'getTableName')) { |
|
119
|
9 |
|
$newTable = $model->getTableName(); |
|
120
|
30 |
|
} elseif (property_exists($model, 'table')) { |
|
121
|
|
|
// NOTE: To be removed in v1.0.0 |
|
122
|
|
|
$newTable = $model->table; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
30 |
|
return [ $model, strtolower($newTable) ]; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Parses the table name from Describe class. |
|
130
|
|
|
* |
|
131
|
|
|
* @param string $table |
|
132
|
|
|
* @param boolean $isForeignKey |
|
133
|
|
|
* @return string |
|
134
|
|
|
*/ |
|
135
|
|
|
abstract protected function getTableName($table, $isForeignKey = false); |
|
136
|
|
|
} |
|
137
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.