public function init() { parent::init(); $this->i2db = Instance::ensure($this->i2db, Connection::className()); $this->infodb = Instance::ensure($this->infodb, Connection::className()); $this->db46 = Instance::ensure($this->db46, Connection::className()); }
public function __construct($db = 'db') { $this->db = Instance::ensure($db, Connection::className()); $this->generator = new Generator(); $this->dbHelper = new Migration(['db' => $this->db]); $this->generatorConfigurator = new GeneratorConfigurator(); }
protected function setUp() { parent::setUp(); $this->mockApplication(); Yii::$app->set('db', ['class' => Connection::className(), 'dsn' => 'sqlite::memory:']); Yii::$app->db->createCommand()->createTable('session', ['id' => 'string', 'expire' => 'integer', 'data' => 'text', 'user_id' => 'integer'])->execute(); }
/** * Initializes the DB connection component. * This method will initialize the [[db]] property to make sure it refers to a valid DB connection. * @throws InvalidConfigException if [[db]] is invalid. */ public function init() { parent::init(); if (is_string($this->db)) { $this->db = Instance::ensure($this->db, Connection::className()); } }
/** * Initializes the migration. * This method will set [[db]] to be the 'db' application component, if it is `null`. */ public function init() { parent::init(); $this->db = Instance::ensure($this->db, Connection::className()); $this->db->getSchema()->refresh(); $this->db->enableSlaves = false; }
public function init() { parent::init(); $db = Instance::ensure($this->db, Connection::className()); $query = new Query(); $this->ticket = $query->select(['*'])->from($this->table)->createCommand($db)->queryAll(); }
/** * Populates Yii::$app with a new application * The application will be destroyed on tearDown() automatically. * @param array $config The application configuration, if needed * @param string $appClass name of the application class to create */ protected function mockApplication($config = [], $appClass = '\\yii\\console\\Application') { new $appClass(ArrayHelper::merge(['id' => 'testapp', 'basePath' => __DIR__, 'vendorPath' => '../../vendor', 'controllerMap' => ['deferred' => ['class' => DeferredController::className()]], 'components' => ['mutex' => ['class' => 'yii\\mutex\\MysqlMutex', 'autoRelease' => false], 'db' => ['class' => Connection::className(), 'dsn' => 'mysql:host=localhost;dbname=yii2_deferred_tasks', 'username' => 'root', 'password' => ''], 'cache' => ['class' => 'yii\\caching\\FileCache']]], $config)); Yii::$app->cache->flush(); Yii::$app->getDb()->open(); Yii::$app->runAction('migrate/down', [99999, 'interactive' => 0, 'migrationPath' => __DIR__ . '/../src/migrations/']); Yii::$app->runAction('migrate/up', ['interactive' => 0, 'migrationPath' => __DIR__ . '/../src/migrations/']); }
public function exec($aliasPath) { $path = str_replace('/', DIRECTORY_SEPARATOR, \Yii::getAlias($aliasPath)); $this->migrationPath = dirname($path); $this->db = Instance::ensure($this->db, Connection::className()); $this->getNewMigrations(); return $this->migrateUp(basename($path)); }
/** * @inheritdoc */ public function beforeAction($action) { if (parent::beforeAction($action)) { $this->db = Instance::ensure($this->db, Connection::className()); return true; } return false; }
/** * @inheritdoc */ public function init() { parent::init(); $this->db = Instance::ensure($this->db, Connection::className()); if (is_string($this->cache)) { $this->cache = Yii::$app->get($this->cache, false); } }
/** * Initializes the DbMessageSource component. * This method will initialize the [[db]] property to make sure it refers to a valid DB connection. * Configured [[cache]] component would also be initialized. * @throws InvalidConfigException if [[db]] is invalid or [[cache]] is invalid. */ public function init() { parent::init(); $this->db = Instance::ensure($this->db, Connection::className()); if ($this->enableCaching) { $this->cache = Instance::ensure($this->cache, Cache::className()); } }
public function down() { $configs = array_merge($this->configs, ArrayHelper::getValue(Yii::$app->params, 'migration.rbac', [])); $this->db = Instance::ensure($configs['db'], Connection::className()); $this->dropTable($configs['assignmentTable']); $this->dropTable($configs['itemChildTable']); $this->dropTable($configs['itemTable']); $this->dropTable($configs['ruleTable']); }
/** * Database action is responsible for all database related stuff. * Checking given database settings, writing them into a config file. * * (Step 3) */ public function actionDatabase() { $success = FALSE; $errorMsg = ''; $config = Configuration::get(); $param = Configuration::getParam(); $form = new DatabaseForm(); if ($form->load(Yii::$app->request->post())) { if (Yii::$app->request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; return ActiveForm::validate($form); } if ($form->validate()) { $dsn = "mysql:host=" . $form->hostname . ";dbname=" . $form->database; // Create Test DB Connection Yii::$app->set('db', ['class' => Connection::className(), 'dsn' => $dsn, 'username' => $form->username, 'password' => $form->password, 'charset' => 'utf8']); try { Yii::$app->db->open(); // Check DB Connection if (InstallerModule::checkDbConnection()) { // Write Config $config['components']['db']['class'] = Connection::className(); $config['components']['db']['dsn'] = $dsn; $config['components']['db']['username'] = $form->username; $config['components']['db']['password'] = $form->password; $config['components']['db']['charset'] = 'utf8'; // Write config for future use $param['installer']['db']['installer_hostname'] = $form->hostname; $param['installer']['db']['installer_database'] = $form->database; $param['installer']['db']['installer_username'] = $form->username; Configuration::set($config); Configuration::setParam($param); $success = TRUE; return $this->redirect(Yii::$app->urlManager->createUrl('//installer/setup/mailer')); } else { $errorMsg = 'Incorrect configuration'; } } catch (Exception $e) { $errorMsg = $e->getMessage(); } } } else { if (isset($param['installer']['db']['installer_hostname'])) { $form->hostname = $param['installer']['db']['installer_hostname']; } if (isset($param['installer']['db']['installer_database'])) { $form->database = $param['installer']['db']['installer_database']; } if (isset($param['installer']['db']['installer_username'])) { $form->username = $param['installer']['db']['installer_username']; } } return $this->render('database', ['model' => $form, 'success' => $success, 'errorMsg' => $errorMsg]); }
/** * @throws InvalidConfigException */ public function init() { parent::init(); if (empty($this->db)) { throw new InvalidConfigException('UserConfig::db must be set.'); } $this->db = Instance::ensure($this->db, Connection::className()); if ($this->cache !== null) { $this->cache = Instance::ensure($this->cache, Cache::className()); } }
/** * This method is invoked right before an action is to be executed (after all possible filters.) * It checks the existence of the [[migrationPath]]. * * @param \yii\base\Action $action the action to be executed. * * @return boolean whether the action should continue to be executed. */ public function beforeAction($action) { if (parent::beforeAction($action)) { if ($action->id !== 'create') { $this->db = Instance::ensure($this->db, Connection::className()); } return TRUE; } else { return FALSE; } }
/** * Initializes the exporter. * This method will initialize required property values and instantiate collection objects. */ public function init() { parent::init(); $this->db = Instance::ensure($this->db, Connection::className()); $this->initSerializer(); $this->initFormatter(); $this->initElements($this->sessions, Session::className()); $this->initElements($this->dictionaries, Dictionary::className()); $this->initElements($this->events, Event::className()); $this->initElements($this->providers, Provider::className()); $this->initElements($this->parameters, Parameter::className()); }
/** * @inheritdoc */ public static function setUpBeforeClass() { $config = (require __DIR__ . '/data/config.php'); $config = $config[static::$driverName]; $config['class'] = Connection::className(); try { Yii::$app->set('db', $config); Yii::$app->getDb()->open(); } catch (\Exception $e) { Yii::$app->clear('db'); } }
/** * @param null $options * @throws \Exception */ public function __construct($options = null) { $this->db = Yii::$app->db; if (is_array($options)) { if (isset($options['prefix'])) { $this->prefix = $options['prefix']; } if (isset($options['db']) && is_a($options['db'], Connection::className())) { $this->db = $options['db']; } } }
/** * Initialize * @throws \yii\base\InvalidConfigException */ public function init() { parent::init(); $this->db = Instance::ensure($this->db, Connection::className()); $this->authManager = Instance::ensure($this->authManager, BaseManager::className()); $this->user = Instance::ensure($this->user, User::className()); if (empty($this->cache)) { $this->cache = $this->createCacheComponent(); } else { $this->cache = Instance::ensure($this->cache, Cache::className()); } }
/** * This method is invoked right before an action is to be executed (after all possible filters.) * It checks the existence of the db and mailer components. * @param \yii\base\Action $action the action to be executed. * @return boolean whether the action should continue to be executed. */ public function beforeAction($action) { try { if (parent::beforeAction($action)) { $this->db = Instance::ensure($this->db, Connection::className()); $this->mailer = Instance::ensure($this->mailer, BaseMailer::className()); return true; } } catch (Exception $e) { Log::error($e->getMessage(), null, __METHOD__); } return false; }
public static function setUpBeforeClass() { try { Yii::$app->set('db', ['class' => Connection::className(), 'dsn' => 'mysql:host=127.0.0.1;port=3306;dbname=test', 'username' => 'root', 'password' => '', 'charset' => 'utf8']); Yii::$app->db->open(); require_once dirname(__DIR__) . '/../migrations/m150106_015855_initDbConfigTable.php'; $migration = new \m150106_015855_initDbConfigTable(); $migration->up(); } catch (\Exception $e) { echo $e->getMessage(); Yii::$app->clear('db'); } }
/** * This method is invoked right before an action is to be executed (after all possible filters.) * It checks the existence of the db and mailer components. * @param \yii\base\Action $action the action to be executed. * @return boolean whether the action should continue to be executed. */ public function beforeAction($action) { try { if (parent::beforeAction($action)) { $this->db = Instance::ensure($this->db, Connection::className()); $this->mailer = Instance::ensure($this->mailer, BaseMailer::className()); return true; } } catch (Exception $e) { $this->stderr("ERROR: " . $e->getMessage() . "\n"); } return false; }
/** * @inheritdoc */ public static function setUpBeforeClass() { try { Yii::$app->set('db', ['class' => Connection::className(), 'dsn' => 'sqlite::memory:']); Yii::$app->getDb()->open(); $lines = explode(';', file_get_contents(__DIR__ . '/migrations/sqlite.sql')); foreach ($lines as $line) { if (trim($line) !== '') { Yii::$app->getDb()->pdo->exec($line); } } } catch (\Exception $e) { Yii::$app->clear('db'); } }
/** * @inheritdoc */ public static function setUpBeforeClass() { try { Yii::$app->set('db', ['class' => Connection::className(), 'dsn' => 'mysql:host=localhost;dbname=yii2_nested_sets_test', 'username' => 'root', 'password' => '']); Yii::$app->getDb()->open(); $lines = explode(';', file_get_contents(__DIR__ . '/migrations/mysql.sql')); foreach ($lines as $line) { if (trim($line) !== '') { Yii::$app->getDb()->pdo->exec($line); } } } catch (\Exception $e) { Yii::$app->clear('db'); } }
/** * Generates the data needed to determine if dependency has been changed. * This method returns the value of the global state. * @param Cache $cache the cache component that is currently evaluating this dependency * @return mixed the data needed to determine if dependency has been changed. * @throws InvalidConfigException if [[db]] is not a valid application component ID */ protected function generateDependencyData($cache) { $db = Instance::ensure($this->db, Connection::className()); if ($this->sql === null) { throw new InvalidConfigException("DbDependency::sql must be set."); } if ($db->enableQueryCache) { // temporarily disable and re-enable query caching $db->enableQueryCache = false; $result = $db->createCommand($this->sql, $this->params)->queryOne(); $db->enableQueryCache = true; } else { $result = $db->createCommand($this->sql, $this->params)->queryOne(); } return $result; }
public function save($runValidation = true, $attributeNames = null) { if ($runValidation && !$this->validate($attributeNames)) { return false; } $config = $this->getConfig(); $local = $config->getConfigFromLocal(); $db = []; $db['class'] = Connection::className(); $db['dsn'] = "mysql:host=" . $this->hostname . ";dbname=" . $this->database . ";port=3306"; $db['username'] = $this->username; $db['password'] = $this->password; $db['charset'] = 'utf8'; $local["components"]["db"] = $db; $config->setConfigToLocal($local); return true; }
public function init() { parent::init(); if (is_string($this->connection)) { $this->connection = Yii::$app->get($this->connection); } elseif (is_array($this->connection)) { if (!isset($this->connection['class'])) { $this->connection['class'] = Connection::className(); } $this->connection = Yii::createObject($this->connection); } if (!$this->connection instanceof Connection) { throw new InvalidConfigException("Queue::connection must be application component ID of a SQL connection."); } if (!$this->hasTable()) { $this->createTable(); } }
public function actionDb() { $this->stdout("Creating \"config/db.php\"... ", Console::FG_YELLOW); $passwordBits = 128; $length = (int) ceil($passwordBits / 8); $binary = random_bytes($length); // PHP 7 native random_bytes() or compat-lib's one $password = substr(strtr(base64_encode($binary), '+/=', '_-.'), 0, $length); $dsnOptions = ['host' => 'localhost', 'port' => '5432', 'dbname' => 'ikaloglog']; $options = ['class' => \yii\db\Connection::className(), 'dsn' => $this->makeDsn('pgsql', $dsnOptions), 'username' => 'ikaloglog', 'password' => $password, 'charset' => 'UTF-8']; $file = "<?php\n"; $file .= "return [\n"; foreach ($options as $k => $v) { $file .= " '{$k}' => '" . addslashes($v) . "',\n"; } $file .= "];\n"; file_put_contents(__DIR__ . '/../config/db.php', $file); $this->stdout("Done.\n", Console::FG_GREEN); }
/** * @throws ExitException * @throws \yii\base\InvalidConfigException */ protected function initDatabase() { if (empty($this->db)) { $this->db = Yii::$app->db; } elseif (is_string($this->db)) { $this->db = Yii::$app->get($this->db); } if (!is_a($this->db, Connection::className())) { throw new ExitException(500, "{$this->db} is not a db connection object", 500); } if (empty($this->database)) { $this->database = Database::className(); } if (is_string($this->database)) { $this->database = new $this->database(['db' => $this->db, 'prefix' => $this->prefix]); } if (!Database::is_a($this->database)) { throw new ExitException(500, "{$this->database} is not a ipv4 database object", 500); } }
public function init() { $this->db = Instance::ensure($this->db, Connection::className()); parent::init(); // Note the default configuration data value will not store to database. $this->data = array_merge($this->loadData(), $this->data); }