112 lines
2.8 KiB
PHP
112 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
class TpaApiDataModel extends Model
|
|
{
|
|
protected $table = 'tpa_api_data';
|
|
protected $primaryKey = 'id';
|
|
protected $useAutoIncrement = true;
|
|
|
|
protected $returnType = 'array';
|
|
protected $useSoftDeletes = false;
|
|
|
|
protected $allowedFields = [
|
|
'file_id',
|
|
'emp_code',
|
|
'name',
|
|
'dob',
|
|
'relation',
|
|
'gender',
|
|
'self',
|
|
'tpa_id',
|
|
'age',
|
|
'is_active',
|
|
'desc',
|
|
'created_by',
|
|
'si',
|
|
'doj',
|
|
'rec_type'
|
|
];
|
|
|
|
// protected $useTimestamps = true;
|
|
// protected $createdField = 'create_at';
|
|
protected $updatedField = null;
|
|
|
|
// protected $dateFormat = 'datetime';
|
|
|
|
// protected $validationRules = [
|
|
// 'file_id' => '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
|
|
];
|
|
}
|
|
|
|
}
|