1
   <?php

class dbLayer
{

    //connection from here 

    private $done;

    function __construct(PDO $connection)
    {
        $this->done = $connection;

    }

    public function createAction()
    {
        //create a new  item
    }

    public function readAction()
    {
        //read all the  items
    }

    public function updateAction()
    {

        $sql = $this->done->prepare("UPDATE `sync_log` SET `Sync_Status`=? WHERE `randKey`=?");
        $sql->execute(array(
            $status,
            $randKey
        ));
    }

    public function deleteAction()
    {
        //delete a item
    }

}

?>

我想$pdo->beginTransaction();与类方法一起使用。如果我的更新因updateAction()方法失败,我如何使用 roalback() ?

4

1 回答 1

3

交易updateAction()示例:

public function updateAction()
{
    try {
        $this->done->beginTransaction();
        $sql = $this->done->prepare("UPDATE `sync_log` SET `Sync_Status`=? WHERE `randKey`=?");
        $sql->execute(array(
            $status,
            $randKey
        ));
        $this->done->commit();
    } catch (PDOException $e) {
        $this->done->rollback();
    }
}

请注意,如果要返回受影响的行数,则必须在提交后执行此操作。并且您想在回滚后返回 0 或false阻塞catch

于 2013-11-08T04:19:56.710 回答