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
This commit is contained in:
mahdahar 2026-02-10 16:43:52 +07:00
parent a9384fbe96
commit 64646293dc

View File

@ -119,9 +119,20 @@ class PatientModel extends BaseModel {
$modelPatCom = new PatComModel(); $modelPatCom = new PatComModel();
$modelPatIdt = new PatIdtModel(); $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 (!empty($input['Custodian'])) {
if (is_array($input['Custodian']) && isset($input['Custodian']['InternalPID'])) { if (is_array($input['Custodian'])) {
$input['Custodian'] = (int) $input['Custodian']['InternalPID']; $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(); $db->transBegin();
try { try {
$this->insert($input); $this->insert($input);
$newInternalPID = $this->getInsertID(); $newInternalPID = $this->getInsertID();
$this->checkDbError($db, 'Insert patient'); $this->checkDbError($db, 'Insert patient');
if (!empty($input['PatIdt'])) { if (!empty($patIdt)) {
$modelPatIdt->createPatIdt($input['PatIdt'], $newInternalPID); $modelPatIdt->createPatIdt($patIdt, $newInternalPID);
$this->checkDbError($db, 'Insert PatIdt'); $this->checkDbError($db, 'Insert PatIdt');
} }
if (!empty($input['PatCom'])) { if (!empty($patCom)) {
$modelPatCom->createPatCom($input['PatCom'], $newInternalPID); $modelPatCom->createPatCom($patCom, $newInternalPID);
$this->checkDbError($db, 'Insert PatCom'); $this->checkDbError($db, 'Insert PatCom');
} }
if (!empty($input['PatAtt'])) { if (!empty($patAtt)) {
$modelPatAtt->createPatAtt($input['PatAtt'], $newInternalPID); $modelPatAtt->createPatAtt($patAtt, $newInternalPID);
$this->checkDbError($db, 'Insert PatAtt'); $this->checkDbError($db, 'Insert PatAtt');
} }
@ -171,8 +181,11 @@ class PatientModel extends BaseModel {
$modelPatAtt = new PatAttModel(); $modelPatAtt = new PatAttModel();
if (!empty($input['Custodian'])) { if (!empty($input['Custodian'])) {
if (is_array($input['Custodian']) && isset($input['Custodian']['InternalPID'])) { if (is_array($input['Custodian'])) {
$input['Custodian'] = (int) $input['Custodian']['InternalPID']; $input['Custodian'] = $input['Custodian']['InternalPID'] ?? null;
if ($input['Custodian'] !== null) {
$input['Custodian'] = (int) $input['Custodian'];
}
} }
} }