Skip to content

Commit

Permalink
Add FKC to menu_items table (#1132)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackcoder87 authored Dec 4, 2024
1 parent 2de72ff commit d75f491
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
27 changes: 26 additions & 1 deletion application/modules/admin/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ public function getInstallSql(): string
`target` VARCHAR(50) NULL DEFAULT NULL,
`module_key` VARCHAR(255) NULL DEFAULT NULL,
`access` VARCHAR(255) NOT NULL DEFAULT "",
PRIMARY KEY (`id`)
PRIMARY KEY (`id`),
INDEX `FK_[prefix]_menu_items_[prefix]_menu` (`menu_id`) USING BTREE,
CONSTRAINT `FK_[prefix]_menu_items_[prefix]_menu` FOREIGN KEY (`menu_id`) REFERENCES `[prefix]_menu` (`id`) ON UPDATE NO ACTION ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS `[prefix]_boxes` (
Expand Down Expand Up @@ -1151,6 +1153,29 @@ public function getUpdate(string $installedVersion): string
$this->db()->query('ALTER TABLE `[prefix]_modules_php_extensions` ADD CONSTRAINT `FK_[prefix]_modules_php_extensions_[prefix]_modules` FOREIGN KEY (`key`) REFERENCES `[prefix]_modules` (`key`) ON UPDATE NO ACTION ON DELETE CASCADE;');
}

// Add FKC to the menu_items table.
// Delete orphaned menu items.
$menuIds = $this->db()->select('id')
->from('menu')
->execute()
->fetchList();

$menuItemsIds = $this->db()->select('menu_id')
->from('menu_items')
->execute()
->fetchList();

$orphanedRows = array_diff($menuItemsIds ?? [], $menuIds ?? []);
if (count($orphanedRows) > 0) {
$this->db()->delete()->from('menu_items')
->where(['menu_id' => $orphanedRows])
->execute();
}

if (!$this->db()->queryCell("SELECT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='" . $dbname . "' AND table_name='[prefix]_menu_items' AND constraint_name='FK_[prefix]_menu_items_[prefix]_menu');")) {
$this->db()->query('ALTER TABLE `[prefix]_menu_items` ADD CONSTRAINT `FK_[prefix]_menu_items_[prefix]_menu` FOREIGN KEY (`menu_id`) REFERENCES `[prefix]_menu` (`id`) ON UPDATE NO ACTION ON DELETE CASCADE;');
}

break;
}

Expand Down
1 change: 0 additions & 1 deletion application/modules/admin/controllers/admin/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ public function indexAction()
if ($this->getRequest()->getPost('delete')) {
$id = (int)$this->getRequest()->getParam('menu');
$menuMapper->delete($id);
$menuMapper->deleteItemsByMenuId($id);
$this->redirect(['action' => 'index']);
}

Expand Down
1 change: 1 addition & 0 deletions application/modules/admin/mappers/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ public function delete(int $id)
$this->db()->delete('menu')
->where(['id' => $id])
->execute();
// Rows in menu_items get deleted due to a FKC.

// Truncate table if this was the last menu. This will also reset AUTO_INCREMENT.
if (!$this->getMenus()) {
Expand Down

0 comments on commit d75f491

Please sign in to comment.