Welcome Guest, Not a member yet? Register   Sign In
Transactions with Model
#1

(This post was last modified: 11-23-2022, 10:38 PM by kenjis.)

Hi, everyone knows how to use transaction, right?

Code:
<?php

$this->db->transStart();
$this->db->query('AN SQL QUERY...');
$this->db->query('ANOTHER QUERY...');
$this->db->query('AND YET ANOTHER QUERY...');
$this->db->transComplete();

But the truth is will be more useful to use transaction with the model. Something Like This:

Code:
$UserModel->db->transStart();

$UserModel->save($user);
$ProfileModel->save($profile);

$UserModel->db->transComplete();

But this approach seems not work. Anyone advice?
Reply
#2

Please read this and you may need to use the if/else structure to test the transaction.

CodeIgniter 4 User Guide - Working with Databases - Transactions - Running Transactions Manually
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

....Meh
Try to make the question in a different way: can you please post an example of transactions using Model object ? thanks
Reply
#4

Your sample code should work.
What's wrong exactly?
Reply
#5

(11-23-2022, 10:40 PM)kenjis Wrote: Your sample code should work.
What's wrong exactly?

Transaction will not consider validation rules failure as a fail query so if we put a data that didn't mach validationRules it will not perform query but neither trow an error on transaction and the transaction will be committed. Maybe i should open a ticket on ci4 github about that?
Reply
#6

Transaction has nothing to do with validation in model. So your code does not work correctly.
I think Model should throw an exception if a validation error occurs.

> Maybe i should open a ticket on ci4 github about that?

Yes.
Reply
#7

But the current behavior does not seem a bug.

Quote:Now, whenever you call the insert(), update(), or save() methods, the data will be validated. If it fails, the model will return boolean false. You can use the errors() method to retrieve the validation errors:
https://codeigniter4.github.io/CodeIgnit...validation

According to the documentation, developers need to check if there are validation errors.
Reply
#8

I found a solution. The code below:

Code:
$UserModel->db->transStart();

$UserModel->save($user);
$ProfileModel->save($profile);

$UserModel->db->transComplete();

Turn into:

Code:
$this->db->transBegin();

if (! $UserModel->save($user)){
$failed = true;
}

if (! $ProfileModel->save($user)){
$failed = true;
}if ($this->db->transStatus() === false || $failed) {
    $this->db->transRollback();
} else {
    $this->db->transCommit();
}


That's all. Enjoy
Reply
#9

(11-27-2022, 01:37 AM)RedWd Wrote: I found a solution. The code below:

Code:
$UserModel->db->transStart();

$UserModel->save($user);
$ProfileModel->save($profile);

$UserModel->db->transComplete();

Turn into:

Code:
$this->db->transBegin();

if (! $UserModel->save($user)){
$failed = true;
}

if (! $ProfileModel->save($user)){
$failed = true;
}if ($this->db->transStatus() === false || $failed) {
    $this->db->transRollback();
} else {
    $this->db->transCommit();
}


That's all. Enjoy

$this->db, $userModel and $profilModel are using a same connection?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB