How to use PHP Objects and Mysql Transaction Control?

Asked

Viewed 365 times

1

I have a database with some tables, and I created a class for manipulation for each table of the bank, an example of them is a class for orders and another for itensPedidos. Each class manipulates its table, and when a class needs to record data in another table, I make an instance of it to be able to change the data, which so far I was able to do without any problem.

My problem started when I went to separate some things that were being carried out together, for example the order class was recording the items directly in the itensPedido table. And from this point on, I don’t know how to proceed. The transaction control I perform works perfectly when I create all the instructions within the same object:

private function inserir()
{
    if( !$this->verificaDados() ):
        return false;
    endif;

    $this->getCon();
    try{
        $this->con->beginTransaction();
        $s = $this->con->prepare( "INSERT INTO pedidos(cliente, total, situacao, data) VALUES(:cliente, :total, :situacao, now())" );
        $s->bindValue( ':cliente', $this->cliente->getId() );
        $s->bindValue( ':total', $this->total );
        $s->bindValue( ':situacao', $this->situacao );
        $s->execute();

        $pedido = $this->con->lastInsertId();

        $st = $this->con->prepare( "INSERT INTO itenspedido(pedido, produto, quantidade, preco, subtotal) VALUES(:pedido, :produto, :quantidade, :preco, :subtotal)" );

        foreach( $this->itens as $item ):
            $st->bindValue( ':pedido', $pedido );
            $st->bindValue( ':produto', $item->getProduto()->getId() );
            $st->bindValue( ':quantidade', $item->getQuantidade() );
            $st->bindValue( ':preco', str_replace( ',', '.', $item->getPreco() ) );
            $st->bindValue( ':subtotal', str_replace( ',', '.', $item->getTotal() ) );
            $st->execute();
        endforeach;

        $this->con->commit();

        return true;
    }catch( PDOException $e ){
        echo 'Erro: ' . $e;
        $this->con->rollBack();

        return false;
    }
}

But the point is that I want each class to manipulate its own table, but I don’t know how to put transaction control in a way that works together with other objects:

try{
    $this->con->beginTransaction();
    $s = $this->con->prepare( "INSERT INTO pedidos(cliente, total, situacao, data) VALUES(:cliente, :total, :situacao, now())" );
    $s->bindValue( ':cliente', $this->cliente->getId() );
    $s->bindValue( ':total', $this->total );
    $s->bindValue( ':situacao', $this->situacao );
    $s->execute();

    $pedido = $this->con->lastInsertId();

        foreach( $this->itens as $item ):
            $item->setPedido($pedido);
            $item->salvar();
        endforeach;

        $this->con->commit();

        return true;
    }catch( PDOException $e ){
        echo 'Erro: ' . $e;
        $this->con->rollBack();

        return false;
    }

Within each save method, I also have beginTransaction(), commit() and rollback() instructions. The request should not be saved if it failed to enter one of the order items.

Someone could help solve this problem?

How I would implement transaction control in Mysql having influence on other classes' Inserts within the transaction.

I appreciate the help you can give me.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.