CDbTransaction

优质
小牛编辑
126浏览
2023-12-01
所有包 | 属性 | 方法
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();
}

公共属性

隐藏继承属性

属性类型描述定义在
activeboolean返回是否这个事务是激活状态CDbTransaction
connectionCDbConnection返回这个事务的数据库连接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 属性 只读 public CDbConnection getConnection()

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

方法详细

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

构造函数。

参见

  • CDbConnection::beginTransaction
commit() 方法
public void commit()
源码: framework/db/CDbTransaction.php#60 (显示) publicfunctioncommit()
{
if($this->_active&&$this->_connection->getActive())
{
Yii::trace('Committingtransaction','system.db.CDbTransaction');
$this->_connection->getPdoInstance()->commit();
$this->_active=false;
}
else
thrownewCDbException(Yii::t('yii','CDbTransactionisinactiveandcannotperformcommitorrollbackoperations.'));
}

提交一个事务。

getActive() 方法
public boolean getActive()
{return}boolean返回是否这个事务是激活状态
源码: framework/db/CDbTransaction.php#99 (显示) publicfunctiongetActive()
{
return$this->_active;
}
getConnection() 方法
public CDbConnection getConnection()
{return}CDbConnection返回这个事务的数据库连接
源码: framework/db/CDbTransaction.php#91 (显示) publicfunctiongetConnection()
{
return$this->_connection;
}
rollback() 方法
public void rollback()
源码: framework/db/CDbTransaction.php#76 (显示) publicfunctionrollback()
{
if($this->_active&&$this->_connection->getActive())
{
Yii::trace('Rollingbacktransaction','system.db.CDbTransaction');
$this->_connection->getPdoInstance()->rollBack();
$this->_active=false;
}
else
thrownewCDbException(Yii::t('yii','CDbTransactionisinactiveandcannotperformcommitorrollbackoperations.'));
}

回滚一个事务。

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