Yii Framework v1.1.10 类参考

CDbTransaction

system.db
继承 class CDbTransaction » CComponent
源自 1.0
版本 $Id: CDbTransaction.php 3426 2011-10-25 00:01:09Z alexander.makarow $
源码 framework/db/CDbTransaction.php
CDbTransaction表示一个数据库事务。

它通常通过调用CDbConnection::beginTransaction创建。

以下代码是使用事务的一种常见情形:
$transaction=$connection->beginTransaction();
try
{
   $connection->createCommand($sql1)->execute();
   $connection->createCommand($sql2)->execute();
   //.... other SQL executions
   $transaction->commit();
}
catch(Exception $e)
{
   $transaction->rollBack();
}

公共属性

隐藏继承属性

属性类型描述定义在
active boolean 返回是否这个事务是激活状态 CDbTransaction
connection CDbConnection 返回这个事务的数据库连接 CDbTransaction

公共方法

隐藏继承方法

方法描述定义在
__call() 如果类中没有调的方法名,则调用这个方法。 CComponent
__construct() 构造函数。 CDbTransaction
__get() 返回一个属性值、一个事件处理程序列表或一个行为名称。 CComponent
__isset() 检查一个属性是否为null。 CComponent
__set() 设置一个组件的属性值。 CComponent
__unset() 设置一个组件的属性为null。 CComponent
asa() 返回这个名字的行为对象。 CComponent
attachBehavior() 附加一个行为到组件。 CComponent
attachBehaviors() 附加一个行为列表到组件。 CComponent
attachEventHandler() 为事件附加一个事件处理程序。 CComponent
canGetProperty() 确定属性是否可读。 CComponent
canSetProperty() 确定属性是否可写。 CComponent
commit() 提交一个事务。 CDbTransaction
detachBehavior() 从组件中分离一个行为。 CComponent
detachBehaviors() 从组件中分离所有行为。 CComponent
detachEventHandler() 分离一个存在的事件处理程序。 CComponent
disableBehavior() 禁用一个附加行为。 CComponent
disableBehaviors() 禁用组件附加的所有行为。 CComponent
enableBehavior() 启用一个附加行为。 CComponent
enableBehaviors() 启用组件附加的所有行为。 CComponent
evaluateExpression() 计算一个PHP表达式,或根据组件上下文执行回调。 CComponent
getActive() 返回返回是否这个事务是激活状态 CDbTransaction
getConnection() 返回返回这个事务的数据库连接 CDbTransaction
getEventHandlers() 返回一个事件的附加处理程序列表。 CComponent
hasEvent() 确定一个事件是否定义。 CComponent
hasEventHandler() 检查事件是否有附加的处理程序。 CComponent
hasProperty() 确定属性是否被定义。 CComponent
raiseEvent() 发起一个事件。 CComponent
rollback() 回滚一个事务。 CDbTransaction

受保护方法

隐藏继承方法

方法描述定义在
setActive() 设置是否这个事务是激活状态 CDbTransaction

属性详细

active 属性
public boolean getActive()
protected void setActive(boolean $value)

返回是否这个事务是激活状态

connection 属性 只读

返回这个事务的数据库连接

方法详细

__construct() 方法
public void __construct(CDbConnection $connection)
$connection CDbConnection 关联到该事务的数据库连接
源码: framework/db/CDbTransaction.php#50 (显示)
public function __construct(CDbConnection $connection)
{
    
$this->_connection=$connection;
    
$this->_active=true;
}

构造函数。

commit() 方法
public void commit()
源码: framework/db/CDbTransaction.php#60 (显示)
public function commit()
{
    if(
$this->_active && $this->_connection->getActive())
    {
        
Yii::trace('Committing transaction','system.db.CDbTransaction');
        
$this->_connection->getPdoInstance()->commit();
        
$this->_active=false;
    }
    else
        throw new 
CDbException(Yii::t('yii','CDbTransaction is inactive and cannot perform commit or roll back operations.'));
}

提交一个事务。

getActive() 方法
public boolean getActive()
{return} boolean 返回是否这个事务是激活状态
源码: framework/db/CDbTransaction.php#99 (显示)
public function getActive()
{
    return 
$this->_active;
}

getConnection() 方法
public CDbConnection getConnection()
{return} CDbConnection 返回这个事务的数据库连接
源码: framework/db/CDbTransaction.php#91 (显示)
public function getConnection()
{
    return 
$this->_connection;
}

rollback() 方法
public void rollback()
源码: framework/db/CDbTransaction.php#76 (显示)
public function rollback()
{
    if(
$this->_active && $this->_connection->getActive())
    {
        
Yii::trace('Rolling back transaction','system.db.CDbTransaction');
        
$this->_connection->getPdoInstance()->rollBack();
        
$this->_active=false;
    }
    else
        throw new 
CDbException(Yii::t('yii','CDbTransaction is inactive and cannot perform commit or roll back operations.'));
}

回滚一个事务。

setActive() 方法
protected void setActive(boolean $value)
$value boolean 是否这个事务是激活状态
源码: framework/db/CDbTransaction.php#107 (显示)
protected function setActive($value)
{
    
$this->_active=$value;
}

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.