public static function init()
 {
     if (self::$_started) {
         return;
     }
     $redirection_conf = sfConfig::get('app_redirection_databases', null);
     if ($redirection_conf === null) {
         return;
     }
     //Iterate over the databases configurations.
     foreach ($redirection_conf as $dbName => $dbOptions) {
         //Check if there are any slaves servers configured.
         if ($dbOptions['slaves'] === null) {
             continue;
         }
         foreach ($dbOptions['slaves'] as &$slave) {
             foreach ($slave as &$param) {
                 $param = sfConfigHandler::replaceConstants($param);
             }
             #Symfony uses 'username' but Propel expects 'user'.
             $slave['user'] = $slave['username'];
             unset($slave['username']);
         }
         self::$_slaveConfig[$dbName] = $dbOptions['slaves'];
         //Check if there is any entity that maybe be redirected to the slave.
         if ($dbOptions['entities'] === null) {
             continue;
         }
         //Iterate over the entities.
         foreach ($dbOptions['entities'] as $model => $options) {
             $peerClass = "{$model}Peer";
             //Check if the peer exits.
             if (!class_exists($peerClass)) {
                 continue;
             }
             $doSelectStmtHook = "{$peerClass}:doSelectStmt:doSelectStmt";
             $doCountHook = "{$peerClass}:doCount:doCount";
             //Register the interceptor function on the peer hooks.
             $interceptor = array('sfPropelRedirection', 'slaveConnection');
             sfMixer::register($doSelectStmtHook, $interceptor);
             sfMixer::register($doCountHook, $interceptor);
             //Check if the peer has conditions in order to be redirected to the slave.
             if (!isset($options['conditions'])) {
                 continue;
             }
             self::$_peerOptions[$peerClass]['conditions'] = $options['conditions'];
             //If there are zero conditions then we don't need to check a gen_column.
             if (!isset($options['gen_column'])) {
                 continue;
             }
             $columnName = strtolower($model) . '.' . strtoupper($options['gen_column']);
             //Check if the gen column really exists in the model
             if (!in_array($columnName, $peerClass::getFieldNames(BasePeer::TYPE_COLNAME))) {
                 continue;
             }
             self::$_peerOptions[$peerClass]['gen_column'] = $columnName;
         }
     }
     self::$_started = true;
 }
Exemplo n.º 2
0
<?php

require_once dirname(__FILE__) . '\\..\\lib\\sfPropelRedirection.class.php';
sfPropelRedirection::init();