|
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
|
|
|
* |
|
25
|
|
|
* @param string|\Rougin\Wildfire\CodeigniterModel $table |
|
26
|
|
|
* @param object $row |
|
27
|
|
|
* @return array |
|
28
|
|
|
*/ |
|
29
|
36 |
|
protected function createObject($table, $row) |
|
30
|
|
|
{ |
|
31
|
36 |
|
list($tableName, $model) = ModelHelper::createInstance($table); |
|
32
|
|
|
|
|
33
|
36 |
|
$properties = []; |
|
34
|
|
|
|
|
35
|
36 |
|
if ($model instanceof CodeigniterModel) { |
|
36
|
36 |
|
$properties = $model->getProperties(); |
|
37
|
36 |
|
$properties = $model->getRelationshipProperties($properties); |
|
38
|
36 |
|
} |
|
39
|
|
|
|
|
40
|
36 |
|
$tableInfo = $this->describe->getTable($tableName); |
|
41
|
|
|
|
|
42
|
36 |
|
$this->setModelFields($model, $row, $properties, $tableInfo); |
|
43
|
|
|
|
|
44
|
30 |
|
return $model; |
|
45
|
6 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Finds the row from the specified ID or with the list of delimiters from |
|
49
|
|
|
* the specified table. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $table |
|
52
|
|
|
* @param array|integer $delimiters |
|
53
|
|
|
* @return object|boolean |
|
54
|
|
|
*/ |
|
55
|
|
|
abstract protected function find($table, $delimiters = []); |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Sets the foreign field of the column, if any. |
|
59
|
|
|
* |
|
60
|
|
|
* @param \CI_Model $model |
|
61
|
|
|
* @param \Rougin\Describe\Column $column |
|
62
|
|
|
* @param array $properties |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
36 |
|
protected function setForeignField(\CI_Model $model, Column $column, array $properties) |
|
66
|
|
|
{ |
|
67
|
36 |
|
$columnName = $column->getField(); |
|
68
|
|
|
|
|
69
|
36 |
|
$foreignColumn = $column->getReferencedField(); |
|
70
|
36 |
|
$foreignTable = $column->getReferencedTable(); |
|
71
|
|
|
|
|
72
|
36 |
|
if (in_array($foreignTable, $properties['belongs_to'])) { |
|
73
|
6 |
|
$delimiters = [ $foreignColumn => $model->$columnName ]; |
|
74
|
6 |
|
$foreign = $this->find($foreignTable, $delimiters); |
|
75
|
|
|
|
|
76
|
|
|
if (is_object($foreign)) { |
|
77
|
|
|
$tableName = TableHelper::getNameFromModel($foreign); |
|
78
|
|
|
|
|
79
|
|
|
$model->$tableName = $foreign; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
36 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Sets the model values based on the result row. |
|
86
|
|
|
* |
|
87
|
|
|
* @param \CI_Model &$model |
|
88
|
|
|
* @param object $row |
|
89
|
|
|
* @param array $properties |
|
90
|
|
|
* @param array $tableInformation |
|
91
|
|
|
* @return void |
|
92
|
|
|
*/ |
|
93
|
36 |
|
protected function setModelFields(&$model, $row, $properties, $tableInformation) |
|
94
|
|
|
{ |
|
95
|
36 |
|
foreach ($tableInformation as $column) { |
|
96
|
36 |
|
$key = $column->getField(); |
|
97
|
|
|
|
|
98
|
36 |
|
$inColumns = ! empty($properties['columns']) && ! in_array($key, $properties['columns']); |
|
99
|
36 |
|
$inHiddenColumns = ! empty($properties['hidden']) && in_array($key, $properties['hidden']); |
|
100
|
|
|
|
|
101
|
36 |
|
if ($inColumns || $inHiddenColumns) { |
|
102
|
36 |
|
continue; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
36 |
|
$model->$key = $row->$key; |
|
106
|
|
|
|
|
107
|
36 |
|
$this->setForeignField($model, $column, $properties); |
|
108
|
36 |
|
} |
|
109
|
30 |
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|