'required|integer', // 'temp_code' => 'required|string|max_length[50]', // 'tname' => 'required|string|max_length[150]', // 'tgender' => 'permit_empty|in_list[M,F,O]', // 'tage' => 'permit_empty|integer' // ]; protected $validationMessages = []; protected $skipValidation = false; public function insertBatchWithChunkLog( array $rows,int $chunkSize = 500,?string $logRef = null) { if (empty($rows)) { return [ 'status' => false, 'inserted' => 0, 'failed_batches' => 0, 'message' => 'No data' ]; } $db = \Config\Database::connect(); $this->skipValidation(true); $inserted = 0; $failedBatches = 0; $batchNo = 0; foreach (array_chunk($rows, $chunkSize) as $chunk) { $batchNo++; try { $db->transStart(); $this->insertBatch($chunk); if ($db->transStatus() === false) { throw new \RuntimeException('DB transaction failed'); } $db->transComplete(); $inserted += count($chunk); } catch (\Throwable $e) { $db->transRollback(); $failedBatches++; // 🔴 Batch-level error log log_message('error', json_encode([ 'type' => 'BATCH_INSERT_FAILED', 'table' => $this->table, 'batch_no' => $batchNo, 'batch_size'=> count($chunk), 'ref' => $logRef, 'error' => $e->getMessage() ])); } } return [ 'status' => $failedBatches === 0, 'inserted' => $inserted, 'failed_batches' => $failedBatches, 'total_batches' => $batchNo ]; } }