Welcome Guest, Not a member yet? Register   Sign In
Transactions not work ?
#1

[eluser]Tống Kiện Phi[/eluser]
This is my controller:
Code:
public function index()
    {
        
        $this->welcome->insert(array(
            'title' => 'test title',
            'content' => 'test content'
        ));
        echo 'Done';
    }
    
    public function rollback()
    {
        $this->welcome->rollback();
        echo 'Rolback';
    }
    
    public function commit()
    {
        $this->welcome->commit();
        echo 'Commit';
    }

This is my Model:

Code:
public function insert($data)
    {
        $this->db->trans_begin();
        $this->db->query("
            INSERT INTO test (title, content)
            VALUES('" . $data['title'] . "','" . $data['content'] . "')
        ");
    }
    
    public function rollback()
    {
        $this->db->trans_rollback();
    }
    
    public function commit()
    {
        $this->db->trans_commit();
    }

When i run http://localhost/ci/index.php/welcome it's insert new record into DB, but when i try run http://localhost/ci/index.php/welcome/rollback .. it's not rollback, So what am i doing wrong?
#2

[eluser]sotoz[/eluser]
Hello.

When you are calling the rollback function you have not started any query to rollback. When you call the controller's insert function you do one connection to the database and try to insert something (I bet the insert isn't working either without the commit) but the query doesn't "stay in memory" so you can rollback it anytime by calling a different controller function.

You have to choose to rollback or commit a query at the runtime of the function you do the insert/update whatever.




Theme © iAndrew 2016 - Forum software by © MyBB