fix: Auto-seed official subjects table to satisfy foreign key constraints on courses creation

This commit is contained in:
Hamza-Ayed
2026-08-28 04:03:01 +03:00
parent d82d5a9ab9
commit ab52d44c94
2 changed files with 19 additions and 1 deletions
@@ -7,6 +7,7 @@ use App\Core\Response;
use App\Core\Database;
use App\Core\Security;
use App\Core\Validator;
use App\Services\CurriculumService;
class TeacherController
{
@@ -167,6 +168,7 @@ class TeacherController
*/
public function getCourses(Request $request, Response $response): void
{
CurriculumService::ensureSchema();
$userId = (int)$request->user_id;
$courses = Database::select(
+17 -1
View File
@@ -17,7 +17,23 @@ class CurriculumService
if (self::$schemaChecked) return;
try {
// Check if lessons table has timeline_chapters_json column
// 1. Check if subjects table has data, seed if empty
$subjectsCount = (int)(Database::selectOne("SELECT COUNT(*) as total FROM subjects")['total'] ?? 0);
if ($subjectsCount === 0) {
Database::query(
"INSERT INTO subjects (id, name, code, stream, is_active) VALUES
(1, 'الرياضيات العلمي', 'math_scientific', 'scientific', 1),
(2, 'الثقافة العسكرية والتربية الوطنية', 'military_culture', 'common', 1),
(3, 'الفيزياء', 'physics_scientific', 'scientific', 1),
(4, 'الكيمياء', 'chemistry_scientific', 'scientific', 1),
(5, 'العلوم الحياتية (الأحياء)', 'biology_scientific', 'scientific', 1),
(6, 'اللغة الإنجليزية', 'english_common', 'common', 1),
(7, 'اللغة العربية المشتركة', 'arabic_common', 'common', 1)
ON DUPLICATE KEY UPDATE name = VALUES(name)"
);
}
// 2. Check if lessons table has timeline_chapters_json column
$cols = Database::select("SHOW COLUMNS FROM lessons LIKE 'timeline_chapters_json'");
if (empty($cols)) {
Database::query("ALTER TABLE lessons ADD COLUMN timeline_chapters_json JSON NULL AFTER hls_url");