|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CodexShaper\DBM\Database\Drivers\MongoDB; |
|
4
|
|
|
|
|
5
|
|
|
use CodexShaper\DBM\Models\CollectionField; |
|
6
|
|
|
use CodexShaper\DBM\Models\DBM_Collection; |
|
7
|
|
|
|
|
8
|
|
|
class Collection |
|
9
|
|
|
{ |
|
10
|
|
|
/*@var string*/ |
|
11
|
|
|
protected $collection; |
|
12
|
|
|
/*@var array*/ |
|
13
|
|
|
protected $columns = []; |
|
14
|
|
|
/*@var string*/ |
|
15
|
|
|
protected $name; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* MongoDB collection constuctor. |
|
19
|
|
|
* |
|
20
|
|
|
* @return void |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct($collection) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->collection = $collection; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Add MongoDB collection column. |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $name |
|
31
|
|
|
* @param string $type |
|
32
|
|
|
* |
|
33
|
|
|
* @return $this |
|
34
|
|
|
*/ |
|
35
|
|
|
public function addColumn($name, $type) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->name = $name; |
|
38
|
|
|
|
|
39
|
|
|
$this->columns[$name] = [ |
|
40
|
|
|
'name' => $name, |
|
41
|
|
|
'old_name' => $name, |
|
42
|
|
|
'type' => $type, |
|
43
|
|
|
'index' => '', |
|
44
|
|
|
'default' => null, |
|
45
|
|
|
'extra' => '', |
|
46
|
|
|
'created_at' => now(), |
|
47
|
|
|
'updated_at' => now(), |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Add index. |
|
55
|
|
|
* |
|
56
|
|
|
* @return $this |
|
57
|
|
|
*/ |
|
58
|
|
|
public function index() |
|
59
|
|
|
{ |
|
60
|
|
|
$this->columns[$this->name]['index'] = 'INDEX'; |
|
61
|
|
|
|
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Add unique key. |
|
67
|
|
|
* |
|
68
|
|
|
* @return $this |
|
69
|
|
|
*/ |
|
70
|
|
|
public function unique() |
|
71
|
|
|
{ |
|
72
|
|
|
$this->columns[$this->name]['index'] = 'UNIQUE'; |
|
73
|
|
|
|
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Add default value. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string|bool|int|null $value |
|
81
|
|
|
* |
|
82
|
|
|
* @return $this |
|
83
|
|
|
*/ |
|
84
|
|
|
public function defaultValue($value) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->columns[$this->name]['default'] = $value; |
|
87
|
|
|
|
|
88
|
|
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Add nullable column. |
|
93
|
|
|
* |
|
94
|
|
|
* @return $this |
|
95
|
|
|
*/ |
|
96
|
|
|
public function nullable() |
|
97
|
|
|
{ |
|
98
|
|
|
$this->default(null); |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Save collection and fields. |
|
105
|
|
|
* |
|
106
|
|
|
* @return void |
|
107
|
|
|
*/ |
|
108
|
|
|
public function save() |
|
109
|
|
|
{ |
|
110
|
|
|
if ($collection = DBM_Collection::where('name', $this->collection)->first()) { |
|
111
|
|
|
$collection->fields()->delete(); |
|
112
|
|
|
$collection->delete(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$collection = new DBM_Collection; |
|
116
|
|
|
$collection->name = $this->collection; |
|
117
|
|
|
$collection->old_name = $this->collection; |
|
118
|
|
|
$collection->extra = ''; |
|
119
|
|
|
$collection->created_at = now(); |
|
120
|
|
|
$collection->updated_at = now(); |
|
121
|
|
|
$collection->save(); |
|
122
|
|
|
|
|
123
|
|
|
foreach ($this->columns as $column) { |
|
124
|
|
|
$field = new CollectionField; |
|
125
|
|
|
$field->dbm_collection_id = $collection->_id; |
|
126
|
|
|
foreach ($column as $key => $value) { |
|
127
|
|
|
$field->{$key} = $value; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$field->save(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$this->columns = []; |
|
134
|
|
|
$this->name = ''; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.