Welcome Guest, Not a member yet? Register   Sign In
$this->db->trans_start(TRUE) has no Effect with AR? (Using MySQL, InnoDB)
#1

[eluser]Unknown[/eluser]
Hi there!

I wrote a Model with a create Method which looks like this:

Code:
public function new_device($data, $test_mode = FALSE)
{
    $this->db->trans_start($test_mode);
    $this->db->insert('table', $data);
    $this->db->trans_complete();

    return $this->db->trans_status();
}

I use the $test_mode var in my Controller; setting it to TRUE should validate the insert-Query without executing it.
Unfortunately, this method does always execute the query; setting $test_mode to TRUE has no Effect.

I even tried the following to be sure:

Code:
public function new_device($data, $test_mode = FALSE)
{
    $this->db->trans_start(TRUE);
    $this->db->insert('table', $data);
    $this->db->trans_complete();

    return $this->db->trans_status();
}

But this does also execute the Query and my DB-table grows...

I tried the following which does work:

Code:
public function new_device($data, $test_mode = FALSE)
{
    $this->db->trans_begin();
    $this->db->insert('table', $data);

    if ($this->db->trans_status() === FALSE)
    {
        $this->db->trans_rollback();
    
        return FALSE;
    }
    else
    {
        if ( ! $test_mode)
        {
            $this->db->trans_commit();
        }
        else
        {
            $this->db->trans_rollback();
        }
        
        return TRUE;
    }
}


Do you have any idea why the first two code-snippets don't work as expected? Is it because of the Active Record insert() Method?



Thanks in advance!
#2

[eluser]theprodigy[/eluser]
Quote:public function new_device($data, $test_mode = FALSE)
{
$this->db->trans_start($test_mode);
$this->db->insert('table', $data);
$this->db->trans_complete();

return $this->db->trans_status();
}
Quote:public function new_device($data, $test_mode = FALSE)
{
$this->db->trans_begin();
$this->db->insert('table', $data);

if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();

return FALSE;
}
else
{
if ( ! $test_mode)
{
$this->db->trans_commit();
}
else
{
$this->db->trans_rollback();
}

return TRUE;
}
}

Just for testing, try changing your first one to trans_begin(FALSE) rather then trans_start(FALSE).
There's not much of a difference since trans_start calls trans_begin, but just see what happens.




Theme © iAndrew 2016 - Forum software by © MyBB