public function save() { if ($this->validate()) { $container = new \yii\di\Container(); $config = (require MultisiteHelper::getDbConfig(Yii::getAlias('@app/config/'), $this->domain)); $container->set('db', $config); $configCopy = (require MultisiteHelper::getDbConfig(Yii::getAlias('@app/config/'), $this->domainCopy)); $container->set('dbCopy', $configCopy); /** @var Connection $db */ $db = $container->get('db'); /** @var Connection $dbCopy */ $dbCopy = $container->get('dbCopy'); $transaction = $db->beginTransaction(); try { // rename tables if database not empty $tables = $db->createCommand('SHOW TABLES')->queryAll(PDO::FETCH_COLUMN); $key = rand(1, 100000); foreach ($tables as $table) { $db->createCommand('RENAME TABLE ' . $table . ' TO ' . $key . '_' . $table)->query(); } $tables = $dbCopy->createCommand('SHOW TABLES')->queryAll(PDO::FETCH_COLUMN); foreach ($tables as $table) { $sql = 'CREATE TABLE ' . $this->getDatabase($config['dsn']) . '.' . $table . ' LIKE ' . $this->getDatabase($configCopy['dsn']) . '.' . $table; $dbCopy->createCommand($sql)->query(); $sql = 'INSERT INTO ' . $this->getDatabase($config['dsn']) . '.' . $table . ' SELECT * FROM ' . $this->getDatabase($configCopy['dsn']) . '.' . $table; $dbCopy->createCommand($sql)->query(); } $transaction->commit(); return true; } catch (\Exception $e) { $transaction->rollBack(); throw $e; } } return false; }
<?php $params = (require __DIR__ . '/params.php'); $basePath = dirname(__DIR__); $webroot = dirname($basePath); $config = ['id' => 'app', 'basePath' => $basePath, 'bootstrap' => ['log'], 'language' => 'en-US', 'runtimePath' => $webroot . '/runtime', 'vendorPath' => $webroot . '/vendor', 'components' => ['request' => ['cookieValidationKey' => ''], 'cache' => ['class' => 'yii\\caching\\FileCache'], 'errorHandler' => ['errorAction' => 'site/error'], 'mailer' => ['class' => 'yii\\swiftmailer\\Mailer'], 'assetManager' => ['bundles' => ['yii\\web\\JqueryAsset' => ['js' => [YII_DEBUG ? 'jquery.js' : 'jquery.min.js']], 'yii\\bootstrap\\BootstrapAsset' => ['css' => [YII_DEBUG ? 'css/bootstrap.css' : 'css/bootstrap.min.css']], 'yii\\bootstrap\\BootstrapPluginAsset' => ['js' => [YII_DEBUG ? 'js/bootstrap.js' : 'js/bootstrap.min.js']]]], 'log' => ['traceLevel' => YII_DEBUG ? 3 : 0, 'targets' => [['class' => 'yii\\log\\FileTarget', 'levels' => ['error', 'warning']]]], 'db' => require \yii\easyii\helpers\Multisite::getDbConfig(__DIR__), 'view' => require \yii\easyii\helpers\Multisite::getThemeConfig(__DIR__)], 'params' => $params]; if (YII_ENV_DEV) { // configuration adjustments for 'dev' environment $config['bootstrap'][] = 'debug'; $config['modules']['debug'] = 'yii\\debug\\Module'; $config['bootstrap'][] = 'gii'; $config['modules']['gii'] = 'yii\\gii\\Module'; $config['components']['db']['enableSchemaCache'] = false; } return array_merge_recursive($config, require dirname(__FILE__) . '/../../vendor/vitalik74/easyii/config/easyii.php');