Jelajahi Sumber

Fix to publication

svalavuo 5 hari lalu
induk
melakukan
17fcaf5eba
3 mengubah file dengan 123 tambahan dan 13 penghapusan
  1. 24 0
      fix_slug_issue.sql
  2. 38 13
      includes/publication.php
  3. 61 0
      run_migration.php

+ 24 - 0
fix_slug_issue.sql

@@ -0,0 +1,24 @@
+-- Fix missing slug column issue
+-- Execute this SQL to add the missing 'slug' column to publications table
+
+-- Add slug column if it doesn't exist
+ALTER TABLE publications 
+ADD COLUMN IF NOT EXISTS slug VARCHAR(255) AFTER title;
+
+-- Add index for slug
+ALTER TABLE publications 
+ADD INDEX IF NOT EXISTS idx_publications_slug (slug);
+
+-- Add published_at column if it doesn't exist  
+ALTER TABLE publications 
+ADD COLUMN IF NOT EXISTS published_at TIMESTAMP NULL DEFAULT NULL AFTER created_at;
+
+-- Update existing publications to generate slugs from titles
+UPDATE publications 
+SET slug = LOWER(REPLACE(REPLACE(REPLACE(title, '[^a-z0-9 ]', ''), ' ', '-'), '-', '')) 
+WHERE slug IS NULL OR slug = '';
+
+-- Update published_at for existing published publications
+UPDATE publications 
+SET published_at = created_at 
+WHERE status = 'published' AND published_at IS NULL;

+ 38 - 13
includes/publication.php

@@ -55,14 +55,23 @@ class Publication {
     }
     
     public function getBySlug($slug) {
-        $sql = "SELECT p.*, GROUP_CONCAT(c.name) as categories 
-                FROM publications p 
-                LEFT JOIN publication_categories pc ON p.id = pc.publication_id 
-                LEFT JOIN categories c ON pc.category_id = c.id 
-                WHERE p.slug = ? 
-                GROUP BY p.id";
-        
-        $publication = $this->db->fetch($sql, [$slug]);
+        // Try to get by slug first
+        try {
+            $sql = "SELECT p.*, GROUP_CONCAT(c.name) as categories 
+                    FROM publications p 
+                    LEFT JOIN publication_categories pc ON p.id = pc.publication_id 
+                    LEFT JOIN categories c ON pc.category_id = c.id 
+                    WHERE p.slug = ? 
+                    GROUP BY p.id";
+            
+            $publication = $this->db->fetch($sql, [$slug]);
+        } catch (Exception $e) {
+            // If slug column doesn't exist, fall back to ID-based lookup
+            if (strpos($e->getMessage(), "Unknown column 'slug'") !== false) {
+                return $this->getById($slug);
+            }
+            throw $e;
+        }
         
         if ($publication && $publication['categories']) {
             $publication['categories_array'] = explode(',', $publication['categories']);
@@ -77,9 +86,17 @@ class Publication {
         $this->db->beginTransaction();
         
         try {
-            // Generate slug from title
-            $slug = $this->generateSlug($data['title']);
-            $data['slug'] = $slug;
+            // Try to generate and add slug from title
+            try {
+                $slug = $this->generateSlug($data['title']);
+                $data['slug'] = $slug;
+            } catch (Exception $e) {
+                // If slug column doesn't exist, skip slug generation
+                if (strpos($e->getMessage(), "Unknown column 'slug'") === false) {
+                    throw $e;
+                }
+                // Continue without slug
+            }
             
             // Set published_at if status is published
             if ($data['status'] === 'published' && !isset($data['published_at'])) {
@@ -106,9 +123,17 @@ class Publication {
         $this->db->beginTransaction();
         
         try {
-            // Update slug if title changed
+            // Try to update slug if title changed
             if (isset($data['title'])) {
-                $data['slug'] = $this->generateSlug($data['title'], $id);
+                try {
+                    $data['slug'] = $this->generateSlug($data['title'], $id);
+                } catch (Exception $e) {
+                    // If slug column doesn't exist, skip slug generation
+                    if (strpos($e->getMessage(), "Unknown column 'slug'") === false) {
+                        throw $e;
+                    }
+                    // Continue without slug
+                }
             }
             
             // Set published_at if status changed to published

+ 61 - 0
run_migration.php

@@ -0,0 +1,61 @@
+<?php
+/**
+ * Database Migration Runner
+ * Adds missing 'slug' column to publications table
+ */
+
+require_once 'includes/config.php';
+require_once 'includes/database.php';
+
+try {
+    $db = Database::getInstance();
+    
+    echo "Starting migration: Add slug column to publications table...\n";
+    
+    // Check if slug column already exists
+    $checkSql = "SHOW COLUMNS FROM publications LIKE 'slug'";
+    $result = $db->fetchAll($checkSql);
+    
+    if (empty($result)) {
+        echo "Adding slug column...\n";
+        
+        // Add slug column
+        $db->query("ALTER TABLE publications ADD COLUMN slug VARCHAR(255) AFTER title");
+        
+        // Add index for slug
+        $db->query("ALTER TABLE publications ADD INDEX idx_publications_slug (slug)");
+        
+        echo "Slug column added successfully.\n";
+    } else {
+        echo "Slug column already exists.\n";
+    }
+    
+    // Check if published_at column exists
+    $checkPublishedAt = "SHOW COLUMNS FROM publications LIKE 'published_at'";
+    $resultPublishedAt = $db->fetchAll($checkPublishedAt);
+    
+    if (empty($resultPublishedAt)) {
+        echo "Adding published_at column...\n";
+        $db->query("ALTER TABLE publications ADD COLUMN published_at TIMESTAMP NULL DEFAULT NULL AFTER created_at");
+        echo "Published_at column added successfully.\n";
+    } else {
+        echo "Published_at column already exists.\n";
+    }
+    
+    // Update existing publications to generate slugs
+    echo "Generating slugs for existing publications...\n";
+    $updateSql = "UPDATE publications SET slug = LOWER(REPLACE(REPLACE(REPLACE(title, '[^a-z0-9 ]', ''), ' ', '-'), '-', '')) WHERE slug IS NULL OR slug = ''";
+    $db->query($updateSql);
+    
+    // Update published_at for existing published publications
+    echo "Updating published_at for existing publications...\n";
+    $updatePublishedAtSql = "UPDATE publications SET published_at = created_at WHERE status = 'published' AND published_at IS NULL";
+    $db->query($updatePublishedAtSql);
+    
+    echo "Migration completed successfully!\n";
+    
+} catch (Exception $e) {
+    echo "Migration failed: " . $e->getMessage() . "\n";
+    echo "Please check your database configuration and permissions.\n";
+}
+?>