currently there is no chance to call $db->begin... without
modification. if model tables are stored on different databases:

class AppController extends Controller
{
        var $__transactions = array();

        function __getDbConnections()
        {
                $models = ClassRegistry::mapKeys();
                $dbConnections = array();

                foreach ( $models as $model )
                {
                        $modelClass =& ClassRegistry::getObject($model);
                        $dbConnections[] = $modelClass->useDbConfig;
                }

                return array_unique($dbConnections);
        }

        function __call( $funcName, $funcArgs )
        {
                $allowedFuncNames = array('begin', 'commit', 'rollback');
                $formattedFuncName = low($funcName);

                if ( in_array($formattedFuncName, $allowedFuncNames) )
                {
                        static $dbConnections = array();

                        if ( empty($dbConnections[0]) )
                                $dbConnections = $this->__getDbConnections();

                        foreach ( $dbConnections as $dbConnection )
                        {
                                $db =& ConnectionManager::getDataSource( 
$dbConnection );

                                switch( $formattedFuncName )
                                {
                                        case 'begin':
                                                
$this->__transactions[$dbConnection] = true;
                                                $db->execute('START 
TRANSACTION');
                                        break;

                                        case 'commit':
                                                if ( 
$this->__transactions[$dbConnection] )
                                                {
                                                        
unset($this->__transactions[$dbConnection]);
                                                        $db->execute('COMMIT');
                                                }
                                        break;

                                        case 'rollback':
                                                if ( 
$this->__transactions[$dbConnection] )
                                                {
                                                        
unset($this->__transactions[$dbConnection]);
                                                        
$db->execute('ROLLBACK');
                                                }
                                        break;
                                }
                        }
                        if ( $db->lastError() !== null )
                        {
                                $querys = $db->_queriesLog;
                                $this->log('SQL ERROR: ' . env('REMOTE_ADDR') . 
$db->lastError() .
' -> ' . end($querys));
                                die('please change the transaction sql 
command');
                        }
                }
        }
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to