Welcome Guest, Not a member yet? Register   Sign In
transBegin problem
#1

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.
Reply
#2

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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB