Reputation: 6887
<?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
}
}
?>
I want to use $pdo->beginTransaction();
with class methods.How i use roalback() if my update fail with updateAction()
method ?
Upvotes: 1
Views: 1706
Reputation: 13649
Transactioned updateAction()
example:
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();
}
}
Note that if you want to return the affected rows count you must do that after commit. And you would like to return 0 or false
on catch
block after rollback.
Upvotes: 3