/** * 注册全局的服务,需在 app 或者 其他 module 中的 registerServices 手动调用。 * * @param \Phalcon\DiInterface $di */ public static function registerGlobalServices($di) { $config = $di->getConfig(); // 当用户系统和 if (!empty($config->user->dbAdapter)) { $di->set('userDbMaster', function () use($di, $config) { $slaves = $config->user->dbAdapter->slave; $slaveKey = array_rand($slaves->toArray()); if (!isset($slaves->{$slaveKey}) || count($slaves) < 1) { throw new Exception\RuntimeException(sprintf('No DB slave options found')); } return Engine::diDbAdapter($slaves->{$slaveKey}->adapter, $slaves->{$slaveKey}->toArray(), $di); }); $di->set('userDbSlave', function () use($di, $config) { $slaves = $config->user->dbAdapter->slave; $slaveKey = array_rand($slaves->toArray()); if (!isset($slaves->{$slaveKey}) || count($slaves) < 1) { throw new Exception\RuntimeException(sprintf('No DB slave options found')); } return Engine::diDbAdapter($slaves->{$slaveKey}->adapter, $slaves->{$slaveKey}->toArray(), $di); }); } }