38676-vm/db/add_slug_to_modifiers.php
2026-02-22 22:20:10 +00:00

30 lines
941 B
PHP

<?php
require_once __DIR__ . '/config.php';
$pdo = db();
try {
// Add slug column to modifiers table if it doesn't exist
$pdo->exec("ALTER TABLE modifiers ADD COLUMN slug VARCHAR(100) AFTER name");
echo "Column 'slug' added to 'modifiers' table.\n";
// Populate initial slugs based on names
$stmt = $pdo->query("SELECT id, name FROM modifiers");
$modifiers = $stmt->fetchAll(PDO::FETCH_ASSOC);
$updateStmt = $pdo->prepare("UPDATE modifiers SET slug = ? WHERE id = ?");
foreach ($modifiers as $m) {
$slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '_', $m['name'])));
$updateStmt->execute([$slug, $m['id']]);
}
echo "Initial slugs populated for 'modifiers' table.\n";
} catch (PDOException $e) {
if ($e->getCode() == '42S21') {
echo "Column 'slug' already exists in 'modifiers' table.\n";
} else {
echo "Error: " . $e->getMessage() . "\n";
}
}