Rollback method does not work in mysqli extended class

2

Hello, Because of the PHP version on the server, the fetch all and mysqli begin_transaction methods do not work. To solve this, I created another connection class by extending the MySQLi class and instantiating it.

<?php
namespace App\DB;

class MySQLiConnector extends \mysqli
{
    function __construct($host, $user, $pass, $db)
    {
        parent::__construct($host, $user, $pass, $db);
    }

    public function query($query) {
        if( !$this->real_query($query) ) {
            return false;
        }

        return new MySQLiResultSet($this);
    }

    public function begin_transaction($flags = NULL, $name = NULL)
    {
        $this->autocommit(FALSE);
    }
}

and the mysqliresultset class

<?php
namespace App\DB;

class MySQLiResultSet extends \MySQLi_Result
{
    public function fetch()
    {
        return $this->fetch_assoc();
    }

    public function fetch_all()
    {
        $rows = array();
        while($row = $this->fetch())
        {
            $rows[] = $row;
        }
        return $rows;
    }
}

?>

However, when I try to use the mysqli-> rollback () method when a given query fails, it does not work, the changes are saved anyway.

The code where I try to run the rollback:

        $this->db->begin_transaction();

        $query1 = $this->db->query($sql1);
        $query2 = $this->db->query($sql2);
        $query3 = $this->db->query($sql3);


        if (!$query1 || !$query2 || !$query3) {
           $this->db->rollback();
           throw new ModelException("Erro da base de dados. -> ".$this->db->error);
        } else {
           $this->db->commit();
           return true;
        }

Is there any way to solve this problem?

    
asked by anonymous 17.01.2017 / 19:05

1 answer

4
  

This function does not work with non transactional table types (like MyISAM or ISAM).

As the PHP manual alert. Only tables with the InnoDB or NDB engine support transactions as autocommit(FALSE) will not work with MyIsam tables. On the MySQL site there is a comparative list of the features of each engine.

Another feature that InnoDB has and MyIsam is the option of foreign keys with reference integrity.

Related:

Foreign Key does not respect referential integrity

    
17.01.2017 / 19:44