Skip to content

Commit

Permalink
Fix content table issues
Browse files Browse the repository at this point in the history
  • Loading branch information
engram-design committed Jan 18, 2019
1 parent cbc405b commit 8e00aec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
12 changes: 8 additions & 4 deletions src/fields/SuperTableField.php
Original file line number Diff line number Diff line change
Expand Up @@ -718,11 +718,15 @@ public function beforeSave(bool $isNew): bool
}
}

// Set the old content table before
$configPath = Fields::CONFIG_FIELDS_KEY . '.' . $this->uid . '.settings.contentTable';
$this->contentTable = Craft::$app->getProjectConfig()->get($configPath);
// Set the content table name
if ($this->id) {
$oldField = $fieldsService->getFieldById($this->id);

if ($oldField instanceof self) {
$this->contentTable = $oldField->contentTable;
}
}

// Now see if we need a new one
$this->contentTable = SuperTable::$plugin->getService()->defineContentTableName($this);

return true;
Expand Down
15 changes: 6 additions & 9 deletions src/services/SuperTableService.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,17 +566,14 @@ public function saveSettings(SuperTableField $supertableField, bool $validate =
$transaction = $db->beginTransaction();

try {
// Create the content table first since the block type fields will need it
$configPath = Fields::CONFIG_FIELDS_KEY . '.' . $supertableField->uid . '.settings.contentTable';
$oldContentTable = Craft::$app->getProjectConfig()->get($configPath);
$newContentTable = $supertableField->contentTable;

// Do we need to create/rename the content table?
if (!$db->tableExists($newContentTable)) {
if (!$db->tableExists($supertableField->contentTable)) {
$oldContentTable = $supertableField->oldSettings['contentTable'] ?? null;

if ($oldContentTable && $db->tableExists($oldContentTable)) {
MigrationHelper::renameTable($oldContentTable, $newContentTable);
MigrationHelper::renameTable($oldContentTable, $supertableField->contentTable);
} else {
$this->_createContentTable($newContentTable);
$this->_createContentTable($supertableField->contentTable);
}
}

Expand All @@ -600,7 +597,7 @@ public function saveSettings(SuperTableField $supertableField, bool $validate =

// Save the new ones
$originalContentTable = Craft::$app->getContent()->contentTable;
Craft::$app->getContent()->contentTable = $newContentTable;
Craft::$app->getContent()->contentTable = $supertableField->contentTable;

foreach ($supertableField->getBlockTypes() as $blockType) {
$blockType->fieldId = $supertableField->id;
Expand Down

0 comments on commit 8e00aec

Please sign in to comment.