1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Raystech\StarterKit\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
9
|
|
|
use Illuminate\Database\Query\JoinClause; |
10
|
|
|
|
11
|
|
|
trait HasMeta |
12
|
|
|
{ |
13
|
|
|
protected $metaData = NULL; |
14
|
|
|
|
15
|
|
|
protected function newMeta($meta, $extra = array()) |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
// dd(__class__); |
18
|
|
|
// dd($meta); |
19
|
|
|
$metaModel = new $this->meta_model(); |
|
|
|
|
20
|
|
|
|
21
|
|
|
if($this->meta_global == true) { |
|
|
|
|
22
|
|
|
$metaModel->model_type = __class__; |
23
|
|
|
} |
24
|
|
|
$metaModel->{$this->meta_key_name} = $meta[$this->meta_key_name]; |
|
|
|
|
25
|
|
|
$metaModel->{$this->meta_value_name} = $meta[$this->meta_value_name]; |
|
|
|
|
26
|
|
|
$metaModel->{$this->meta_foreign_key} = $this->{$this->meta_primary_key}; |
|
|
|
|
27
|
|
|
|
28
|
|
|
// if(!empty($extra)) { |
29
|
|
|
// foreach($extra as $key => $value) { |
30
|
|
|
|
31
|
|
|
// } |
32
|
|
|
// } |
33
|
|
|
return $metaModel; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get the data associated with this model |
39
|
|
|
* |
40
|
|
|
* @return hasOne |
|
|
|
|
41
|
|
|
*/ |
42
|
|
|
public function meta() |
43
|
|
|
{ |
44
|
|
|
// return $this->morphMany($this->meta_model, $this->meta_foreign_key, $this->primaryKey); |
45
|
|
|
|
46
|
|
|
return $this->hasMany($this->meta_model, $this->meta_foreign_key, $this->primaryKey); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Dynamically retrieve attributes on the model. |
51
|
|
|
* |
52
|
|
|
* @param string $key |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
|
public function __get($key) |
56
|
|
|
{ |
57
|
|
|
$value = $this->getAttribute($key); |
|
|
|
|
58
|
|
|
|
59
|
|
|
if (is_null($value)) { |
60
|
|
|
try { |
61
|
|
|
if(isset($this->getMeta()->$key)) { |
62
|
|
|
$value = $this->getMeta()->$key->{$this->meta_value_name}; |
|
|
|
|
63
|
|
|
} else { |
64
|
|
|
$value = NULL; |
65
|
|
|
} |
66
|
|
|
} catch (Exception $e) { |
|
|
|
|
67
|
|
|
$value = NULL; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $value; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Dynamically set attributes on the model. |
76
|
|
|
* |
77
|
|
|
* @param string $key |
78
|
|
|
* @param mixed $value |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
public function __set($key, $value) |
82
|
|
|
{ |
83
|
|
|
if (array_key_exists($key, $this->getAttributes())) { |
|
|
|
|
84
|
|
|
$this->setAttribute($key, $value); |
|
|
|
|
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
try { |
89
|
|
|
if (isset($this->getMeta()->$key)) { |
90
|
|
|
$this->getMeta()->$key->{$this->meta_value_name} = $value; |
|
|
|
|
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
} catch (Exception $e) { } |
|
|
|
|
94
|
|
|
|
95
|
|
|
if(\Config::get('database.default') === 'sqlite') { |
96
|
|
|
$columns = \DB::select('PRAGMA table_info('.$this->table.')'); |
|
|
|
|
97
|
|
|
|
98
|
|
|
foreach ($columns as $column) { |
99
|
|
|
if ($column->name === $key) { |
100
|
|
|
$this->setAttribute($key, $value); |
101
|
|
|
return; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} else { |
105
|
|
|
$columns = \DB::getSchemaBuilder()->getColumnListing($this->getTable()); |
|
|
|
|
106
|
|
|
|
107
|
|
|
// $columns = \DB::select('DESCRIBE '.$this->table); |
108
|
|
|
|
109
|
|
|
// dd($columns); |
110
|
|
|
foreach ($columns as $column) { |
111
|
|
|
if ($column === $key) { |
112
|
|
|
$this->setAttribute($key, $value); |
113
|
|
|
return; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
$this->getMeta()->$key = $this->newMeta(array($this->meta_key_name => $key, $this->meta_value_name => $value)); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
public function __isset($key) |
122
|
|
|
{ |
123
|
|
|
if ((isset($this->attributes[$key]) |
|
|
|
|
124
|
|
|
|| isset($this->relations[$key])) |
|
|
|
|
125
|
|
|
|| ($this->hasGetMutator($key) && ! is_null($this->getAttributeValue($key)))) { |
|
|
|
|
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
return isset($this->getMeta()->$key->{$this->meta_value_name}); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function push() |
132
|
|
|
{ |
133
|
|
|
return parent::push() && $this->deleteMeta() && $this->saveMeta(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function save(array $options = array()) |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
return parent::save() && $this->deleteMeta() && $this->saveMeta(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get the data associated with this model in a usable format |
143
|
|
|
* |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
protected function getMeta() |
147
|
|
|
{ |
148
|
|
|
if (is_null($this->metaData)) { |
149
|
|
|
$primaryKey = $this->primaryKey; |
|
|
|
|
150
|
|
|
|
151
|
|
|
if (is_null($this->primaryKey)) { |
152
|
|
|
// If nothing has been set and there is no ID then there will be no data |
153
|
|
|
$this->metaData = (object)[]; |
154
|
|
|
} else { |
155
|
|
|
$meta = $this->meta; |
|
|
|
|
156
|
|
|
|
157
|
|
|
$niceDataArray = array(); |
158
|
|
|
|
159
|
|
|
foreach ($meta as $data) { |
160
|
|
|
$niceDataArray[$data->{$this->meta_key_name}] = $data; |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$this->metaData = (object)$niceDataArray; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
return $this->metaData; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
protected function saveMeta() |
170
|
|
|
{ |
171
|
|
|
$primaryKey = $this->primaryKey; |
|
|
|
|
172
|
|
|
foreach ((array)$this->getMeta() as $data) { |
173
|
|
|
|
174
|
|
|
if (is_null($data->{$this->meta_foreign_key})) { |
|
|
|
|
175
|
|
|
$data->{$this->meta_foreign_key} = $this->$primaryKey; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if(!is_null($data->{$this->meta_value_name})) { |
|
|
|
|
179
|
|
|
$data->save(); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return true; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
protected function deleteMeta() |
187
|
|
|
{ |
188
|
|
|
foreach ((array)$this->getMeta() as $data) { |
189
|
|
|
if(is_null($data->{$this->meta_value_name})) { |
|
|
|
|
190
|
|
|
$dataID = $this->meta_primary_key; |
|
|
|
|
191
|
|
|
$data->destroy($data->$dataID); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return true; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
|
200
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.