ezpdo也有事务处理了,可以支持自动事务回滚和手工回滚
首先看自动回滚的事务
// 获得 EZPDO管理器
$m = epManager::instance();
$os = $m->find("from SomeClass as c where c.var = 'x'");
if (!$os) {
echo "no object found\n";
exit(-1);
}
$o = $os[0];
//用start_t()开始事务
$m->start_t();
// 改变值了
$o->var = 'y';
// 结束事务提交
$m->commit_t(); // by default: auto-rollback
另外是需要用try catch的,手工来回滚
// get the EZPDO manager
$m = epManager::instance();
// find an object using EZOQL
$os = $m->find("from SomeClass as c where c.var = 'x'");
if (!$os) {
echo "no object found\n";
exit(-1);
}
$o = $os[0];
// call manager to start transaction
$m->start_t();
try {
// change the value of var in object
$o->var = 'y';
// end transaction and save changed objects
$m->commit_t(false); // false: no auto rollback
}
catch (Exception $e) {
// something wrong. rollback to old values
$m->rollback_t();
// do something here
// ......
}
// 'y' if transaction successful; 'x' if failed.
echo $o->var;
首先看自动回滚的事务
// 获得 EZPDO管理器
$m = epManager::instance();
$os = $m->find("from SomeClass as c where c.var = 'x'");
if (!$os) {
echo "no object found\n";
exit(-1);
}
$o = $os[0];
//用start_t()开始事务
$m->start_t();
// 改变值了
$o->var = 'y';
// 结束事务提交
$m->commit_t(); // by default: auto-rollback
另外是需要用try catch的,手工来回滚
// get the EZPDO manager
$m = epManager::instance();
// find an object using EZOQL
$os = $m->find("from SomeClass as c where c.var = 'x'");
if (!$os) {
echo "no object found\n";
exit(-1);
}
$o = $os[0];
// call manager to start transaction
$m->start_t();
try {
// change the value of var in object
$o->var = 'y';
// end transaction and save changed objects
$m->commit_t(false); // false: no auto rollback
}
catch (Exception $e) {
// something wrong. rollback to old values
$m->rollback_t();
// do something here
// ......
}
// 'y' if transaction successful; 'x' if failed.
echo $o->var;