1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace carono\yii2migrate; |
4
|
|
|
|
5
|
|
|
use carono\yii2migrate\helpers\SchemaHelper; |
6
|
|
|
use yii\helpers\Inflector; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class PivotColumn |
10
|
|
|
* |
11
|
|
|
* @package carono\yii2installer |
12
|
|
|
*/ |
13
|
|
|
class PivotColumn |
14
|
|
|
{ |
15
|
|
|
protected $_refTable; |
16
|
|
|
protected $_refColumn; |
17
|
|
|
protected $_sourceTable; |
18
|
|
|
protected $_sourceColumn; |
19
|
|
|
protected $_suffix; |
20
|
|
|
protected $_tableName; |
21
|
|
|
protected $_columns = []; |
22
|
|
|
protected $_prefix = 'pv'; |
23
|
|
|
/** |
24
|
|
|
* @var Migration |
25
|
|
|
*/ |
26
|
|
|
public $migrate; |
27
|
|
|
public $column_type = \yii\db\Schema::TYPE_INTEGER; |
28
|
|
|
public $column_length; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
2 |
|
public function __toString() |
34
|
|
|
{ |
35
|
2 |
|
return $this->getPrefix(); |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
public function setPrefix($value) |
39
|
|
|
{ |
40
|
1 |
|
$this->_prefix = $value; |
41
|
1 |
|
} |
42
|
|
|
|
43
|
10 |
|
public function getPrefix() |
44
|
|
|
{ |
45
|
10 |
|
return $this->_prefix; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param $migrate |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
11 |
|
public function setMigrate($migrate) |
53
|
|
|
{ |
54
|
11 |
|
$this->migrate = $migrate; |
55
|
11 |
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param $value |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
11 |
|
public function setSuffix($value) |
63
|
|
|
{ |
64
|
11 |
|
$this->_suffix = $value; |
65
|
11 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param $name |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
5 |
|
public function tableName($name) |
73
|
|
|
{ |
74
|
5 |
|
$this->_tableName = $name; |
75
|
5 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param array $columns |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
6 |
|
public function columns($columns) |
83
|
|
|
{ |
84
|
6 |
|
$this->_columns = $columns; |
85
|
6 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
10 |
|
public function getColumns() |
92
|
|
|
{ |
93
|
10 |
|
return $this->_columns; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
12 |
|
public function getTableName() |
100
|
|
|
{ |
101
|
12 |
|
$name = $this->_tableName ?: implode('_', [$this->getPrefix(), $this->_sourceTable, $this->_suffix]); |
102
|
12 |
|
return '{{%' . SchemaHelper::expandTablePrefix($name, '') . '}}'; |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return mixed |
107
|
|
|
*/ |
108
|
1 |
|
public function getSuffix() |
109
|
|
|
{ |
110
|
1 |
|
return $this->_suffix; |
111
|
|
|
} |
112
|
|
|
|
113
|
8 |
|
public function remove() |
114
|
|
|
{ |
115
|
8 |
|
if ($this->_columns) { |
|
|
|
|
116
|
5 |
|
$this->migrate->downNewColumns([$this->getTableName() => $this->_columns]); |
117
|
|
|
} |
118
|
8 |
|
$this->migrate->dropTable($this->getTableName()); |
119
|
8 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param $value |
123
|
|
|
* @return $this |
124
|
|
|
*/ |
125
|
|
|
public function setType($value) |
126
|
|
|
{ |
127
|
|
|
$this->column_type = $value; |
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param $value |
133
|
|
|
* @return $this |
134
|
|
|
*/ |
135
|
|
|
public function setLength($value) |
136
|
|
|
{ |
137
|
|
|
$this->column_length = $value; |
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
9 |
|
public function apply() |
142
|
|
|
{ |
143
|
|
|
/** |
144
|
|
|
* @var ForeignKeyColumn $type |
145
|
|
|
*/ |
146
|
|
|
$columns = [ |
147
|
9 |
|
$this->getSourceColumn() => $this->migrate->foreignKey($this->getSourceTable()), |
|
|
|
|
148
|
9 |
|
$this->getRefColumn() => $this->migrate->foreignKey($this->getRefTable()), |
|
|
|
|
149
|
|
|
]; |
150
|
9 |
|
$columnsInt = array_combine(array_keys($columns), [ |
151
|
9 |
|
$this->migrate->db->getSchema()->createColumnSchemaBuilder($this->column_type, $this->column_length), |
152
|
9 |
|
$this->migrate->db->getSchema()->createColumnSchemaBuilder($this->column_type, $this->column_length) |
153
|
|
|
]); |
154
|
|
|
|
155
|
9 |
|
$this->migrate->createTable($this->getTableName(), $columnsInt); |
156
|
9 |
|
$this->migrate->addPrimaryKey(null, $this->getTableName(), array_keys($columns)); |
157
|
|
|
|
158
|
9 |
|
foreach ($columns as $name => $type) { |
159
|
9 |
|
$type->sourceTable($this->getTableName())->sourceColumn($name); |
160
|
9 |
|
$type->apply(); |
161
|
|
|
} |
162
|
9 |
|
if ($advancedColumns = $this->getColumns()) { |
|
|
|
|
163
|
5 |
|
$this->migrate->upNewColumns([$this->getTableName() => $this->getColumns()]); |
164
|
|
|
} |
165
|
9 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return null |
169
|
|
|
*/ |
170
|
11 |
|
public function getRefTable() |
171
|
|
|
{ |
172
|
11 |
|
return $this->_refTable; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return mixed |
177
|
|
|
*/ |
178
|
11 |
|
public function getRefColumn() |
179
|
|
|
{ |
180
|
11 |
|
if (!$this->_refColumn) { |
181
|
10 |
|
$name = implode('_', [$this->getRefTable(), 'id']); |
|
|
|
|
182
|
|
|
} else { |
183
|
2 |
|
$name = $this->_refColumn; |
184
|
|
|
} |
185
|
11 |
|
$refColumn = SchemaHelper::expandTablePrefix($name, ''); |
186
|
11 |
|
if (strtolower($refColumn) === strtolower($this->getSourceColumn())) { |
|
|
|
|
187
|
1 |
|
$refColumn = Inflector::singularize($this->_suffix) . '_id'; |
188
|
|
|
} |
189
|
11 |
|
return $refColumn; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return null |
194
|
|
|
*/ |
195
|
13 |
|
public function getSourceTable() |
196
|
|
|
{ |
197
|
13 |
|
return $this->_sourceTable; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return mixed |
202
|
|
|
*/ |
203
|
13 |
|
public function getSourceColumn() |
204
|
|
|
{ |
205
|
13 |
|
if (!$this->_sourceColumn) { |
206
|
12 |
|
$name = implode('_', [$this->getSourceTable(), 'id']); |
|
|
|
|
207
|
|
|
} else { |
208
|
2 |
|
$name = $this->_sourceColumn; |
209
|
|
|
} |
210
|
13 |
|
return SchemaHelper::expandTablePrefix($name, ''); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param $name |
215
|
|
|
* @return $this |
216
|
|
|
*/ |
217
|
13 |
|
public function refTable($name) |
218
|
|
|
{ |
219
|
13 |
|
$this->_refTable = $name; |
220
|
13 |
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param $name |
225
|
|
|
* @return $this |
226
|
|
|
*/ |
227
|
2 |
|
public function sourceColumn($name) |
228
|
|
|
{ |
229
|
2 |
|
$this->_sourceColumn = $name; |
230
|
2 |
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param $name |
235
|
|
|
* @return $this |
236
|
|
|
*/ |
237
|
12 |
|
public function sourceTable($name) |
238
|
|
|
{ |
239
|
12 |
|
$this->_sourceTable = $name; |
240
|
12 |
|
return $this; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param $name |
245
|
|
|
* @return $this |
246
|
|
|
*/ |
247
|
11 |
|
public function refColumn($name) |
248
|
|
|
{ |
249
|
11 |
|
$this->_refColumn = $name; |
250
|
11 |
|
return $this; |
251
|
|
|
} |
252
|
|
|
} |