實物回滾

$m =D( 'YourModel' ); //或者是M();
$m2 =D( 'YouModel2' );
$m ->startTrans(); //在第一個模型裏啓用就能夠了,或者第二個也行
$result = $m ->where( '刪除條件' )-> delete ();
$result2 =m2->where( '刪除條件' )-> delete ();
if ( $result && $result2 ){
$m ->commit(); //成功則提交
} else {
$m ->rollback(); //不成功,則回滾
}
 
$servername = "localhost" ;
$username = "root" ;
$password = "admin" ;
$dbname = "test" ;
try {
   $conn = new PDO( "mysql:host=$servername;dbname=$dbname" , $username , $password );
   $conn ->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
   //開始事務
   $conn ->beginTransaction();
   $conn -> exec ( "INSERT INTO `hello`(`firstname`,`lastname`,`email`)VALUES('YE','XIAMING','yexianming@163.com')" );
   $conn -> exec ( "INSERT INTO `hello`(`firstname`,`lastname`,`email`)VALUES('YE','CONG','yecong@163.com')" );
   $conn -> exec ( "INSERT INTO `hello`(`firstname`,`lastname`,`email`)VALUES('FANG','MENG','fangmeng@168.com')" );
   //提交事務
   $conn ->commit();
   echo "New records created successfully!" ;
} catch (PDOException $e ){
    //回滾事務
    $conn ->rollBack();
    echo $sql . "<br>" . $e ->getMessage();
}
$conn =NULL;
相關文章
相關標籤/搜索