• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

Mark的小试牛刀

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

PHPActiveRecord 学习三

#事务处理 注意事务 数据库要用InnoDB引擎

$c1 = User::connection();

try {
    //开启事务
    $c1->transaction(); 
    //sql语句
    $sql = User::create(
        array("name" => "good_nice")
    );
    //提交事务
    $c1->commit();

} catch (\Exception $e) {
    //事务回滚
    $c1->rollback();
    throw $e;
}    

  源码 lib/Connection.php

abstract class Connection
{
	......
	....
	..
	/**
	 * Starts a transaction.
	 */
	public function transaction()
	{
		if (!$this->connection->beginTransaction())
			throw new DatabaseException($this);
	}

	/**
	 * Commits the current transaction.
	 */
	public function commit()
	{
		if (!$this->connection->commit())
			throw new DatabaseException($this);
	}

	/**
	 * Rollback a transaction.
	 */
	public function rollback()
	{
		if (!$this->connection->rollback())
			throw new DatabaseException($this);
	}
	......
	....
	..
}

  #输出sql语句的三种方法

1. User::table()->conn->last_query  #returns last query to the connection

2. User::connection()->last_query    #same as the first

3. User::table()->last_sql     #this will only return the last query sent to the finder functions and will not include association queries

  

 

posted on 2017-05-11 14:26  我是天才啊  阅读(334)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3