CodeIgniter Forums
transBegin problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: transBegin problem (/showthread.php?tid=92256)



transBegin problem - okatse - 12-31-2024

Hi
I don't know what I'm doing wrong. Without the transaction, the code works correctly. With the transaction, there is an error.   pg_affected_rows(): Argument #1 ($result) must be of type PgSql\Result, bool given
[url=https://www.duckduckgo.com/?q=TypeError+pg_affected_rows%28%29%3A+Argument+%231+%28%24result%29+must+be+of+type+PgSql%5CResult%2C+bool+given][/url]
Code:
public function test2()
{
    $db = \Config\Database::connect();
    $toInsert[] = [
        'name' => 'aaaa'
    ];
    $toInsert[] = [
        'name' => null
    ];
    try {
        $db->transBegin();
        $query = $this->db->table('public.test');
        if ($query->insertBatch($toInsert) === false) {
            d($db->error());
            d('Error from IF');
            $db->transRollback();
        }
        else
        {
            $db->transCommit();
        }
    } catch (DatabaseException $th) {
        d($th->getMessage());
        d('Error from TRY');
        $db->transRollback();
    }
}

name => null - artificial invocation of an incorrect SQL query.

Another issue is that if($query->insertBatch($toInsert) === false) never returns false. Or I don't know what needs to happen for this if statement to be satisfied.


RE: transBegin problem - michalsn - 12-31-2024

Looks like a bug. When the query fails, the resultID is set to false automatically. You can officially report it here: https://github.com/codeigniter4/CodeIgniter4/issues