This helper class ensures a transaction to occure without error. Put your code within a transaction without worrying where the transaction started. If method mA() of Class A start a transaction and another method mB() of Class B also starts a transaction and call the mA() method, an error will occur transaction already started. With this helper you don't have to worry about this.
Requirements ¶
Yii 1.1
Usage ¶
Let say thers is
Class A
{
  public function mA()
  {
    Yii::app()->db->beginTransaction();
    ..
    ..
    Yii::app()->db->currentTransaction->commit();
  }
}
AND
Class B
{
  public function mB()
  {
    $a=new A();
    Yii::app()->db->beginTransaction();
    ..
    ..
    $a->mA(); //error occured
    ..
    ..
    Yii::app()->db->currentTransaction->commit();
  }
}
with TransactionHelper write the above code like below :
Class A
{
  public function mA()
  {
    $trans_key=TransactionHelper::beginTransaction();
    ..
    ..
    TransactionHelper::commit($trans_key);
  }
}
Class B
{
  public function mB()
  {
    $a=new A();
    $trans_key=TransactionHelper::beginTransaction();
    ..
    ..
    $a->mA(); //no error , transaction will not commit here
    ..
    ..
    TransactionHelper::commit($trans_key);
  }
}
Just put the TransactionHelper.php anywhere in your code.load it. Use it.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.