CodeIgniter Forums
multiple transaction codeigniter 4 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: multiple transaction codeigniter 4 (/showthread.php?tid=81814)



multiple transaction codeigniter 4 - devo - 05-01-2022

how can i use transaction of multiple model?

what i try is like this


Code:
$model1 = new Model1;

$model2 = new Model2;

$model3 = new Model3;

$db = \Config\Database::connection();

$db->transBegin();
$a1 = $model1->getData(1);
if($a1){
$model2->insert($id,$data);
$this->another_function();
}

if ($db->transStatus() === false) {
    $db->transRollback();
} else {
    $db->transCommit();
}

Code:
public function another_function() {

    $db = \Config\Database::connect();
    $selext1 = $db->query("SELECT QUERY")->getRowArray();
   
    if ($selext1) {
        $sleect2 = $db->table('table1')->where(['id' => 1])->get()->getRowArray();
        if ($sleect2) {

            $db->table('table3')->insert($data);
               
           
        }
    }
}