Fix: Template delete page refresh + add debug logging
ISSUE: Template delete returns success but record appears not deleted ROOT CAUSE: Delete WAS working (verified in database), but page wasn't refreshing properly after success CHANGES: 1. JavaScript fix (discharge_templates.php): - Replaced conditional load_form_div / location.assign logic - Now uses simple location.reload() after 500ms delay - Ensures page always refreshes after successful delete 2. Controller debug logging (Template.php): - Added comprehensive debug logs to trace delete flow - Logs: request ID, row fetch, query compilation, affected rows - Logs error codes and messages for troubleshooting - Will help diagnose future issues VERIFIED: - Database confirmed template ID 2 was deleted - Only frontend refresh was failing - Now forces reload to show updated list
This commit is contained in:
parent
7587384338
commit
d923dc6c29
|
|
@ -2977,23 +2977,58 @@ HTML;
|
|||
$id = (int) $id;
|
||||
$ok = false;
|
||||
$message = 'Unable to delete template.';
|
||||
|
||||
log_message('debug', 'Delete request for template ID: ' . $id);
|
||||
|
||||
if ($id > 0 && $this->db->tableExists('ipd_discharge_templates')) {
|
||||
$row = $this->db->table('ipd_discharge_templates')
|
||||
->where('id', $id)
|
||||
->get(1)
|
||||
->getRowArray() ?? [];
|
||||
|
||||
if (! empty($row)) {
|
||||
$ok = (bool) $this->db->table('ipd_discharge_templates')->where('id', $id)->delete();
|
||||
log_message('debug', 'Row fetched: ' . json_encode($row));
|
||||
|
||||
if (! $ok) {
|
||||
// Some legacy/live schemas may reject delete via engine constraints; archive instead.
|
||||
$ok = (bool) $this->db->table('ipd_discharge_templates')
|
||||
->where('id', $id)
|
||||
->update(['status' => 0, 'is_default' => 0]);
|
||||
if ($ok) {
|
||||
$message = 'Template archived.';
|
||||
if (! empty($row)) {
|
||||
try {
|
||||
// Try to delete the record
|
||||
$deleteQuery = $this->db->table('ipd_discharge_templates')->where('id', $id);
|
||||
log_message('debug', 'Delete query: ' . $deleteQuery->getCompiledDelete(false));
|
||||
|
||||
$deleteQuery->delete();
|
||||
$affectedRows = $this->db->affectedRows();
|
||||
log_message('debug', 'Delete affected rows: ' . $affectedRows);
|
||||
|
||||
if ($affectedRows > 0) {
|
||||
$ok = true;
|
||||
$message = 'Template deleted.';
|
||||
log_message('debug', 'Delete successful');
|
||||
} else {
|
||||
// Delete didn't affect any rows - try archive instead
|
||||
log_message('error', 'Delete affected 0 rows for template ID: ' . $id);
|
||||
|
||||
$updateQuery = $this->db->table('ipd_discharge_templates')->where('id', $id);
|
||||
log_message('debug', 'Archive query: ' . $updateQuery->getCompiledUpdate(['status' => 0, 'is_default' => 0], false));
|
||||
|
||||
$updateResult = $updateQuery->update(['status' => 0, 'is_default' => 0]);
|
||||
$updateAffected = $this->db->affectedRows();
|
||||
|
||||
log_message('debug', 'Archive affected rows: ' . $updateAffected);
|
||||
|
||||
if ($updateResult && $updateAffected > 0) {
|
||||
$ok = true;
|
||||
$message = 'Template archived.';
|
||||
} else {
|
||||
log_message('error', 'Archive also affected 0 rows for template ID: ' . $id);
|
||||
$dbError = $this->db->error();
|
||||
log_message('error', 'Database error code: ' . $dbError['code'] . ', message: ' . $dbError['message']);
|
||||
$message = 'Database error: ' . ($dbError['message'] ?? 'No rows affected');
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
log_message('error', 'Exception during delete: ' . $e->getMessage());
|
||||
log_message('error', 'Exception trace: ' . $e->getTraceAsString());
|
||||
$message = 'Exception: ' . $e->getMessage();
|
||||
$ok = false;
|
||||
}
|
||||
|
||||
if ($ok && (int) ($row['is_default'] ?? 0) === 1) {
|
||||
|
|
@ -3014,9 +3049,15 @@ HTML;
|
|||
if ($ok && $message === 'Unable to delete template.') {
|
||||
$message = 'Template deleted.';
|
||||
}
|
||||
} else {
|
||||
log_message('error', 'Row not found for template ID: ' . $id);
|
||||
}
|
||||
} else {
|
||||
log_message('error', 'Invalid ID or table does not exist. ID: ' . $id . ', Table exists: ' . ($this->db->tableExists('ipd_discharge_templates') ? 'yes' : 'no'));
|
||||
}
|
||||
|
||||
log_message('debug', 'Final result - OK: ' . ($ok ? 'true' : 'false') . ', Message: ' . $message);
|
||||
|
||||
if ($this->request->isAJAX()) {
|
||||
return $this->response->setJSON([
|
||||
'update' => $ok ? 1 : 0,
|
||||
|
|
|
|||
|
|
@ -641,11 +641,10 @@ $status = (int) ($edit['status'] ?? 1);
|
|||
|
||||
setNotice('success', data.error_text || 'Template deleted successfully.');
|
||||
|
||||
if (typeof load_form_div === 'function') {
|
||||
load_form_div('<?= base_url('setting/template/discharge_templates') ?>', 'maindiv', 'IPD Discharge Template');
|
||||
} else {
|
||||
window.location.assign('<?= base_url('setting/template/discharge_templates') ?>');
|
||||
}
|
||||
// Force page reload to show updated list
|
||||
setTimeout(function() {
|
||||
window.location.reload();
|
||||
}, 500);
|
||||
})
|
||||
.catch(function () {
|
||||
setNotice('danger', 'Network error while deleting template.');
|
||||
|
|
|
|||
50
check_table_structure.php
Normal file
50
check_table_structure.php
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
// Quick script to check table structure
|
||||
define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR);
|
||||
|
||||
// Bootstrap CI4
|
||||
require __DIR__ . '/vendor/autoload.php';
|
||||
$pathsConfig = new Config\Paths();
|
||||
$bootstrap = \CodeIgniter\Boot::bootWeb($pathsConfig);
|
||||
$app = $bootstrap->getCodeIgniter();
|
||||
|
||||
// Get database connection
|
||||
$db = \Config\Database::connect();
|
||||
|
||||
// Check table creation SQL
|
||||
$query = $db->query("SHOW CREATE TABLE ipd_discharge_templates");
|
||||
$row = $query->getRowArray();
|
||||
|
||||
echo "Table Structure:\n";
|
||||
echo "================\n\n";
|
||||
echo $row['Create Table'] . "\n\n";
|
||||
|
||||
// Check for foreign key references
|
||||
$query = $db->query("
|
||||
SELECT
|
||||
TABLE_NAME,
|
||||
COLUMN_NAME,
|
||||
CONSTRAINT_NAME,
|
||||
REFERENCED_TABLE_NAME,
|
||||
REFERENCED_COLUMN_NAME
|
||||
FROM
|
||||
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
|
||||
WHERE
|
||||
REFERENCED_TABLE_NAME = 'ipd_discharge_templates'
|
||||
AND TABLE_SCHEMA = DATABASE()
|
||||
");
|
||||
|
||||
echo "\nForeign Keys Referencing This Table:\n";
|
||||
echo "====================================\n\n";
|
||||
$results = $query->getResultArray();
|
||||
if (count($results) > 0) {
|
||||
foreach ($results as $row) {
|
||||
echo "Table: {$row['TABLE_NAME']}\n";
|
||||
echo "Column: {$row['COLUMN_NAME']}\n";
|
||||
echo "Constraint: {$row['CONSTRAINT_NAME']}\n";
|
||||
echo "References: {$row['REFERENCED_TABLE_NAME']}.{$row['REFERENCED_COLUMN_NAME']}\n\n";
|
||||
}
|
||||
} else {
|
||||
echo "No foreign keys reference this table.\n";
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user