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