9
9
use craft \db \Query ;
10
10
use craft \db \Table ;
11
11
use craft \fields \MatrixField ;
12
+ use craft \fields \MissingField ;
12
13
use craft \helpers \Db ;
13
14
use craft \helpers \Json ;
14
15
use craft \helpers \MigrationHelper ;
@@ -62,8 +63,9 @@ public function safeUp()
62
63
63
64
if ($ contentTable ) {
64
65
if (!$ this ->db ->tableExists ($ contentTable )) {
66
+ $ this ->_createContentTable ($ settings , $ field );
65
67
// Re-save field
66
- $ superTableService ->saveSettings ($ fieldsService ->getFieldById ($ field ['id ' ]));
68
+ // $superTableService->saveSettings($fieldsService->getFieldById($field['id']));
67
69
}
68
70
} else {
69
71
$ this ->_createContentTable ($ settings , $ field );
@@ -186,7 +188,9 @@ public function safeUp()
186
188
foreach ($ superTableBlockTypes as $ superTableBlockType ) {
187
189
$ correctFieldColumns = [];
188
190
$ dbFieldColumns = [];
191
+ $ missingFields = false ;
189
192
193
+ $ superTableField = $ fieldsService ->getFieldById ($ superTableBlockType ['fieldId ' ]);
190
194
$ fieldLayout = $ fieldsService ->getLayoutById ($ superTableBlockType ['fieldLayoutId ' ]);
191
195
192
196
// Find what the columns should be according to the block type fields
@@ -195,12 +199,23 @@ public function safeUp()
195
199
if ($ field ::hasContentColumn ()) {
196
200
$ correctFieldColumns [] = 'field_ ' . $ field ->handle ;
197
201
}
202
+
203
+ if (get_class ($ field ) == MissingField::class) {
204
+ $ missingFields = true ;
205
+ echo " > Unable to update {$ superTableField ->contentTable } as it contains missing fields. Please fix your missing fields first ... \n" ;
206
+ break ;
207
+ }
198
208
}
199
209
}
200
210
201
- $ superTableField = $ fieldsService ->getFieldById ($ superTableBlockType ['fieldId ' ]);
211
+ // If there are any missing fields, we have to quit right now, otherwise we'll mess up
212
+ // the content table, as we just don't know enough about the content table structure
213
+ // to reliably update it properly.
214
+ if ($ missingFields ) {
215
+ continue ;
216
+ }
202
217
203
- if ($ superTableField && get_class ( $ superTableField ) === SuperTableField::class ) {
218
+ if ($ superTableField ) {
204
219
$ contentTable = $ superTableField ->contentTable ;
205
220
206
221
if ($ contentTable ) {
@@ -212,6 +227,10 @@ public function safeUp()
212
227
}
213
228
}
214
229
230
+ // Sort items the same - just in case they're in a slightly different order, but all there
231
+ sort ($ correctFieldColumns );
232
+ sort ($ dbFieldColumns );
233
+
215
234
if ($ correctFieldColumns != $ dbFieldColumns ) {
216
235
$ fieldsService ->saveField ($ superTableField );
217
236
@@ -277,15 +296,20 @@ private function _createContentTable($settings, $field)
277
296
$ settings ['contentTable ' ] = $ contentTable ;
278
297
$ this ->update (Table::FIELDS , ['settings ' => Json::encode ($ settings )], ['id ' => $ field ['id ' ]]);
279
298
280
- // Also update our local copy of the field so we can save it
281
- $ newField ->contentTable = $ contentTable ;
299
+ // Create the actual content table
300
+ if (!$ this ->db ->tableExists ($ contentTable )) {
301
+ $ migration = new CreateSuperTableContentTable ([
302
+ 'tableName ' => $ contentTable ,
303
+ ]);
282
304
283
- echo " > Local field table name { $ newField -> contentTable } ... \n" ;
305
+ $ migration -> up () ;
284
306
285
- // Re-save field - for good measure
286
- $ superTableService ->saveSettings ($ newField );
307
+ echo " > Created table {$ contentTable } ... \n\n" ;
308
+ } else {
309
+ echo " > Content table {$ contentTable } already exists, skipping ... \n\n" ;
287
310
288
- echo " > Updated Super Table field after content table creation ... \n\n" ;
311
+ return ;
312
+ }
289
313
}
290
314
291
315
private function _getContentTableName (SuperTableField $ field ): string
0 commit comments