1 | <?php |
||
2 | |||
3 | namespace carono\yii2migrate; |
||
4 | |||
5 | |||
6 | use carono\yii2migrate\exceptions\ForeignKeyException; |
||
7 | use carono\yii2migrate\helpers\SchemaHelper; |
||
8 | use yii\db\ColumnSchemaBuilder; |
||
9 | |||
10 | /** |
||
11 | * Class ForeignKeyColumn |
||
12 | * |
||
13 | * @package carono\yii2installer |
||
14 | */ |
||
15 | class ForeignKeyColumn extends ColumnSchemaBuilder |
||
16 | { |
||
17 | const FK_CASCADE = 'CASCADE'; |
||
18 | const FK_DEFAULT = 'SET DEFAULT'; |
||
19 | const FK_NULL = 'SET NULL'; |
||
20 | const FK_RESTRICT = 'RESTRICT'; |
||
21 | const FK_NO_ACTION = 'NO ACTION'; |
||
22 | |||
23 | protected $_onDelete = self::FK_CASCADE; |
||
24 | protected $_onUpdate; |
||
25 | protected $_refTable; |
||
26 | protected $_refColumn; |
||
27 | protected $_sourceTable; |
||
28 | protected $_sourceColumn; |
||
29 | protected $_name; |
||
30 | /** |
||
31 | * @var Migration |
||
32 | */ |
||
33 | public $migrate; |
||
34 | |||
35 | /** |
||
36 | * @param $migrate |
||
37 | * @return $this |
||
38 | */ |
||
39 | 51 | public function setMigrate($migrate) |
|
40 | { |
||
41 | 51 | $this->migrate = $migrate; |
|
42 | 51 | $this->db = $migrate->db; |
|
43 | 51 | return $this; |
|
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param $value |
||
48 | * @return $this |
||
49 | */ |
||
50 | 2 | public function setName($value) |
|
51 | { |
||
52 | 2 | $this->_name = $value; |
|
53 | 2 | return $this; |
|
54 | } |
||
55 | |||
56 | /** |
||
57 | * @return mixed |
||
58 | */ |
||
59 | 44 | public function getName() |
|
60 | { |
||
61 | 44 | return $this->_name; |
|
62 | } |
||
63 | |||
64 | 44 | public function formIndexName() |
|
65 | { |
||
66 | 44 | if ($this->_name) { |
|
67 | $name = $this->_name; |
||
68 | } else { |
||
69 | 44 | $name = $this->formName($this->getSourceTable(), $this->getSourceColumn(), $this->getRefTable(), $this->getRefColumn()); |
|
70 | } |
||
71 | 44 | $name = $this->migrate->expandTablePrefix($name); |
|
72 | 44 | return $name; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * @throws ForeignKeyException |
||
77 | */ |
||
78 | 42 | public function apply() |
|
79 | { |
||
80 | 42 | $this->migrate->addForeignKey( |
|
81 | 42 | $this->getName(), $this->getSourceTable(), $this->getSourceColumn(), $this->getRefTable(), |
|
82 | 42 | $this->getRefColumn(), $this->getOnDelete(), $this->getOnUpdate() |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
83 | ); |
||
84 | 42 | } |
|
85 | |||
86 | 1 | public function remove() |
|
87 | { |
||
88 | 1 | $this->migrate->dropForeignKeyByColumn($this->getSourceTable(), $this->getSourceColumn()); |
|
89 | 1 | } |
|
90 | |||
91 | /** |
||
92 | * @return null|string |
||
93 | */ |
||
94 | 46 | public function getRefTable() |
|
95 | { |
||
96 | 46 | return $this->_refTable; |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param $table |
||
101 | * @return mixed |
||
102 | * @throws ForeignKeyException |
||
103 | */ |
||
104 | 33 | protected function getRefColumnByTable($table) |
|
105 | { |
||
106 | 33 | if (!$this->db->getTableSchema($table, true)) { |
|
107 | 1 | throw new ForeignKeyException("Ref table '$table' not found"); |
|
108 | } |
||
109 | 33 | $pk = current($this->migrate->db->getTableSchema($table)->primaryKey); |
|
110 | 33 | if (!$pk){ |
|
111 | throw new ForeignKeyException("Primary key not found in ref table '$table'"); |
||
112 | } |
||
113 | 33 | return $pk; |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * @return null|string |
||
118 | */ |
||
119 | 47 | public function getRefColumn() |
|
120 | { |
||
121 | 47 | if (!$this->_refColumn && $this->migrate) { |
|
122 | 33 | $this->_refColumn = $this->getRefColumnByTable($this->getRefTable()); |
|
123 | } |
||
124 | 47 | return $this->_refColumn; |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * @return null|string |
||
129 | */ |
||
130 | 45 | public function getSourceTable() |
|
131 | { |
||
132 | 45 | return $this->_sourceTable; |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * @return null|string |
||
137 | */ |
||
138 | 45 | public function getSourceColumn() |
|
139 | { |
||
140 | 45 | return $this->_sourceColumn; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * @return string |
||
145 | */ |
||
146 | 43 | public function getOnDelete() |
|
147 | { |
||
148 | 43 | return $this->_onDelete; |
|
149 | } |
||
150 | |||
151 | /** |
||
152 | * @return null|string |
||
153 | */ |
||
154 | 42 | public function getOnUpdate() |
|
155 | { |
||
156 | 42 | return $this->_onUpdate; |
|
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param $string |
||
161 | * @return $this |
||
162 | */ |
||
163 | 5 | public function onDelete($string) |
|
164 | { |
||
165 | 5 | $this->_onDelete = $string; |
|
166 | 5 | return $this; |
|
167 | } |
||
168 | |||
169 | /** |
||
170 | * @param $string |
||
171 | * @return $this |
||
172 | */ |
||
173 | 4 | public function onUpdate($string) |
|
174 | { |
||
175 | 4 | $this->_onUpdate = $string; |
|
176 | 4 | return $this; |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * @return ForeignKeyColumn |
||
181 | */ |
||
182 | 2 | public function onDeleteCascade() |
|
183 | { |
||
184 | 2 | return $this->onDelete(self::FK_CASCADE); |
|
185 | } |
||
186 | |||
187 | /** |
||
188 | * @return ForeignKeyColumn |
||
189 | */ |
||
190 | 2 | public function onDeleteNull() |
|
191 | { |
||
192 | 2 | return $this->onDelete(self::FK_NULL); |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * @return ForeignKeyColumn |
||
197 | */ |
||
198 | 1 | public function onDeleteDefault() |
|
199 | { |
||
200 | 1 | return $this->onDelete(self::FK_DEFAULT); |
|
201 | } |
||
202 | |||
203 | /** |
||
204 | * @return ForeignKeyColumn |
||
205 | */ |
||
206 | 1 | public function onDeleteRestrict() |
|
207 | { |
||
208 | 1 | return $this->onDelete(self::FK_RESTRICT); |
|
209 | } |
||
210 | |||
211 | /** |
||
212 | * @return ForeignKeyColumn |
||
213 | */ |
||
214 | 1 | public function onDeleteNoAction() |
|
215 | { |
||
216 | 1 | return $this->onDelete(self::FK_NO_ACTION); |
|
217 | } |
||
218 | |||
219 | /** |
||
220 | * @return ForeignKeyColumn |
||
221 | */ |
||
222 | 1 | public function onUpdateCascade() |
|
223 | { |
||
224 | 1 | return $this->onUpdate(self::FK_CASCADE); |
|
225 | } |
||
226 | |||
227 | /** |
||
228 | * @return ForeignKeyColumn |
||
229 | */ |
||
230 | 1 | public function onUpdateNull() |
|
231 | { |
||
232 | 1 | return $this->onUpdate(self::FK_NULL); |
|
233 | } |
||
234 | |||
235 | /** |
||
236 | * @return ForeignKeyColumn |
||
237 | */ |
||
238 | public function onUpdateDefault() |
||
239 | { |
||
240 | return $this->onUpdate(self::FK_DEFAULT); |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * @return ForeignKeyColumn |
||
245 | */ |
||
246 | 1 | public function onUpdateRestrict() |
|
247 | { |
||
248 | 1 | return $this->onUpdate(self::FK_RESTRICT); |
|
249 | } |
||
250 | |||
251 | /** |
||
252 | * @return ForeignKeyColumn |
||
253 | */ |
||
254 | 1 | public function onUpdateNoAction() |
|
255 | { |
||
256 | 1 | return $this->onUpdate(self::FK_NO_ACTION); |
|
257 | } |
||
258 | |||
259 | /** |
||
260 | * @param $name |
||
261 | * @return $this |
||
262 | */ |
||
263 | 50 | public function refTable($name) |
|
264 | { |
||
265 | 50 | $this->_refTable = $name; |
|
266 | 50 | return $this; |
|
267 | } |
||
268 | |||
269 | /** |
||
270 | * @param $name |
||
271 | * @return $this |
||
272 | */ |
||
273 | 49 | public function sourceColumn($name) |
|
274 | { |
||
275 | 49 | $this->_sourceColumn = $name; |
|
276 | 49 | return $this; |
|
277 | } |
||
278 | |||
279 | /** |
||
280 | * @param $table |
||
281 | * @param $column |
||
282 | * @param $refTable |
||
283 | * @param $refColumn |
||
284 | * @return string |
||
285 | */ |
||
286 | 45 | public function formName($table, $column, $refTable, $refColumn) |
|
287 | { |
||
288 | 45 | $table = SchemaHelper::removeSchema($this->migrate->expandTablePrefix($table)); |
|
289 | 45 | $refTable = SchemaHelper::removeSchema($this->migrate->expandTablePrefix($refTable)); |
|
290 | 45 | return "{$table}[{$column}]_{$refTable}[{$refColumn}]_fk"; |
|
291 | } |
||
292 | |||
293 | /** |
||
294 | * @param $name |
||
295 | * |
||
296 | * @return $this |
||
297 | */ |
||
298 | 49 | public function sourceTable($name) |
|
299 | { |
||
300 | 49 | $this->_sourceTable = $name; |
|
301 | 49 | return $this; |
|
302 | } |
||
303 | |||
304 | /** |
||
305 | * @param $name |
||
306 | * @return $this |
||
307 | */ |
||
308 | 49 | public function refColumn($name) |
|
309 | { |
||
310 | 49 | $this->_refColumn = $name; |
|
311 | 49 | return $this; |
|
312 | } |
||
313 | } |