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