Difference between #3 and #4 of
How to use nested DB transactions (MySQL 5+, PostgreSQL)

Revision #4 has been created by samdark on Aug 6, 2011, 1:03:08 PM with the memo:

Updated configuration for 1.1.8
« previous (#3) next (#5) »

Changes

Title changed

How to use nested DB transactions (MySqlQL 5+, PgSqlostgreSQL)

Category changed

TutorialHow-tos

Yii version unchanged

Tags unchanged

db, mysql, postgresql, transaction, nested

Content changed

sSource code taken from: [PHP, PDO & Nested Transactions](http://www.kennynet.co.uk/2008/12/02/php-pdo-nested-transactions/). tTested with: MySqlQL 5.1.30 + Yii 1.1.8.

Problem:
[...]
With default Yii implementation, PDO will throw exception saying there already is active transaction.

But there's a solution
 :)

First, you'll need to extend PDO class and save it in your the protected/components directory:
[...]
```

Then, you'll need to alter the behaviour of [CDbConnection::createPdoInstance()]. You can do this by making a subclass of it in protected/components/NestedDbConnection.php
 
 
There, change $pdoClass to
Now your class name (NestedPDO in this example):
 
 
```php 
class NestedDbConnection extends CDbConnection
 
{
 
    protected function createPdoInstance()
 
    {
 
        $pdoClass='NestedPdo';
 
        if(($pos=strpos($this->connectionString,':'))!==false)
 
        {
 
            $driver=strtolower(substr($this->connectionString,0,$pos));
 
            if($driver==='mssql' || $driver==='dblib')
 
              $pdoClass='CMssqlPdoAdapter';
 
        }
 
        return new $pdoClass($this->connectionString,$this->username,
 
                             $this->password,$this->attributes);
 
    }
 
}
 
```
 
 
Note that $this->_attributes was also changed to $this->attributes so the subclassing will work.
 
 
Now you can add the class name to the
an use it in db configuration array in `protected/config/main.php`: ```php 'db'=>array(        'c'pdoClass' => 'NestedDbConnection',
 
       
PDO',
 
'connectionString' => ...         ), ``` That's it, there you go ;)
41 0
26 followers
Viewed: 67 909 times
Version: 1.1
Category: How-tos
Written by: mindeh
Last updated by: Boaz
Created on: Jun 30, 2009
Last updated: 9 years ago
Update Article

Revisions

View all history