From 64646293dcde867bf79d837f2f152d5ad7d3de21 Mon Sep 17 00:00:00 2001 From: mahdahar <89adham@gmail.com> Date: Tue, 10 Feb 2026 16:43:52 +0700 Subject: [PATCH] Fix patient creation error: extract nested arrays before insert - Extract PatIdt, PatCom, PatAtt arrays before patient insert to prevent MySQL error 1241 - Fix Custodian handling when InternalPID is null - Apply same fix to updatePatient method --- app/Models/Patient/PatientModel.php | 35 ++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/app/Models/Patient/PatientModel.php b/app/Models/Patient/PatientModel.php index 5d26bdc..540cea5 100644 --- a/app/Models/Patient/PatientModel.php +++ b/app/Models/Patient/PatientModel.php @@ -119,9 +119,20 @@ class PatientModel extends BaseModel { $modelPatCom = new PatComModel(); $modelPatIdt = new PatIdtModel(); + // Extract nested data before filtering + $patIdt = $input['PatIdt'] ?? null; + $patCom = $input['PatCom'] ?? null; + $patAtt = $input['PatAtt'] ?? null; + + // Remove nested arrays that don't belong to patient table + unset($input['PatIdt'], $input['PatCom'], $input['PatAtt']); + if (!empty($input['Custodian'])) { - if (is_array($input['Custodian']) && isset($input['Custodian']['InternalPID'])) { - $input['Custodian'] = (int) $input['Custodian']['InternalPID']; + if (is_array($input['Custodian'])) { + $input['Custodian'] = $input['Custodian']['InternalPID'] ?? null; + if ($input['Custodian'] !== null) { + $input['Custodian'] = (int) $input['Custodian']; + } } } @@ -134,23 +145,22 @@ class PatientModel extends BaseModel { $db->transBegin(); try { - $this->insert($input); $newInternalPID = $this->getInsertID(); $this->checkDbError($db, 'Insert patient'); - if (!empty($input['PatIdt'])) { - $modelPatIdt->createPatIdt($input['PatIdt'], $newInternalPID); + if (!empty($patIdt)) { + $modelPatIdt->createPatIdt($patIdt, $newInternalPID); $this->checkDbError($db, 'Insert PatIdt'); } - if (!empty($input['PatCom'])) { - $modelPatCom->createPatCom($input['PatCom'], $newInternalPID); + if (!empty($patCom)) { + $modelPatCom->createPatCom($patCom, $newInternalPID); $this->checkDbError($db, 'Insert PatCom'); } - if (!empty($input['PatAtt'])) { - $modelPatAtt->createPatAtt($input['PatAtt'], $newInternalPID); + if (!empty($patAtt)) { + $modelPatAtt->createPatAtt($patAtt, $newInternalPID); $this->checkDbError($db, 'Insert PatAtt'); } @@ -171,8 +181,11 @@ class PatientModel extends BaseModel { $modelPatAtt = new PatAttModel(); if (!empty($input['Custodian'])) { - if (is_array($input['Custodian']) && isset($input['Custodian']['InternalPID'])) { - $input['Custodian'] = (int) $input['Custodian']['InternalPID']; + if (is_array($input['Custodian'])) { + $input['Custodian'] = $input['Custodian']['InternalPID'] ?? null; + if ($input['Custodian'] !== null) { + $input['Custodian'] = (int) $input['Custodian']; + } } }