"Fossies" - the Fresh Open Source Software Archive

Member "yii-1.1.24.a5ab20/framework/db/CDbTransaction.php" (7 Jun 2021, 2959 Bytes) of package /linux/www/yii-1.1.24.a5ab20.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "CDbTransaction.php" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.1.23.445827_vs_1.1.24.a5ab20.

    1 <?php
    2 /**
    3  * CDbTransaction class file
    4  *
    5  * @author Qiang Xue <qiang.xue@gmail.com>
    6  * @link http://www.yiiframework.com/
    7  * @copyright 2008-2013 Yii Software LLC
    8  * @license http://www.yiiframework.com/license/
    9  */
   10 
   11 /**
   12  * CDbTransaction represents a DB transaction.
   13  *
   14  * It is usually created by calling {@link CDbConnection::beginTransaction}.
   15  *
   16  * The following code is a common scenario of using transactions:
   17  * <pre>
   18  * $transaction=$connection->beginTransaction();
   19  * try
   20  * {
   21  *    $connection->createCommand($sql1)->execute();
   22  *    $connection->createCommand($sql2)->execute();
   23  *    //.... other SQL executions
   24  *    $transaction->commit();
   25  * }
   26  * catch(Exception $e)
   27  * {
   28  *    $transaction->rollback();
   29  * }
   30  * </pre>
   31  *
   32  * @property CDbConnection $connection The DB connection for this transaction.
   33  * @property boolean $active Whether this transaction is active.
   34  *
   35  * @author Qiang Xue <qiang.xue@gmail.com>
   36  * @package system.db
   37  * @since 1.0
   38  */
   39 class CDbTransaction extends CComponent
   40 {
   41     private $_connection=null;
   42     private $_active;
   43 
   44     /**
   45      * Constructor.
   46      * @param CDbConnection $connection the connection associated with this transaction
   47      * @see CDbConnection::beginTransaction
   48      */
   49     public function __construct(CDbConnection $connection)
   50     {
   51         $this->_connection=$connection;
   52         $this->_active=true;
   53     }
   54 
   55     /**
   56      * Commits a transaction.
   57      * @throws CException if the transaction or the DB connection is not active.
   58      */
   59     public function commit()
   60     {
   61         if($this->_active && $this->_connection->getActive())
   62         {
   63             Yii::trace('Committing transaction','system.db.CDbTransaction');
   64             if($this->_connection->getPdoInstance()->inTransaction())
   65                 $this->_connection->getPdoInstance()->commit();
   66             $this->_active=false;
   67         }
   68         else
   69             throw new CDbException(Yii::t('yii','CDbTransaction is inactive and cannot perform commit or roll back operations.'));
   70     }
   71 
   72     /**
   73      * Rolls back a transaction.
   74      * @throws CException if the transaction or the DB connection is not active.
   75      */
   76     public function rollback()
   77     {
   78         if($this->_active && $this->_connection->getActive())
   79         {
   80             Yii::trace('Rolling back transaction','system.db.CDbTransaction');
   81             if($this->_connection->getPdoInstance()->inTransaction())
   82                 $this->_connection->getPdoInstance()->rollBack();
   83             $this->_active=false;
   84         }
   85         else
   86             throw new CDbException(Yii::t('yii','CDbTransaction is inactive and cannot perform commit or roll back operations.'));
   87     }
   88 
   89     /**
   90      * @return CDbConnection the DB connection for this transaction
   91      */
   92     public function getConnection()
   93     {
   94         return $this->_connection;
   95     }
   96 
   97     /**
   98      * @return boolean whether this transaction is active
   99      */
  100     public function getActive()
  101     {
  102         return $this->_active;
  103     }
  104 
  105     /**
  106      * @param boolean $value whether this transaction is active
  107      */
  108     protected function setActive($value)
  109     {
  110         $this->_active=$value;
  111     }
  112 }