30 lines
687 B
PHP
30 lines
687 B
PHP
<?php
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
class TransactionService
|
|
{
|
|
private $CI;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->CI =& get_instance();
|
|
}
|
|
|
|
public function run(callable $callback)
|
|
{
|
|
$this->CI->db->trans_begin();
|
|
|
|
try {
|
|
$result = $callback();
|
|
if ($this->CI->db->trans_status() === false) {
|
|
throw new RuntimeException('Operasi database gagal.');
|
|
}
|
|
$this->CI->db->trans_commit();
|
|
return $result;
|
|
} catch (Throwable $exception) {
|
|
$this->CI->db->trans_rollback();
|
|
throw $exception;
|
|
}
|
|
}
|
|
}
|