/** * Connects to the specified schema and assigns it to all models which need it. * * @param $schema schema * @return CDbConnection */ protected function connectDb($schema) { // Assign request $this->request = Yii::app()->getRequest(); // Check parameter if (is_null($schema)) { $this->db = null; return null; } // Connect to database $connectionString = 'mysql:host=' . Yii::app()->user->host . ';port=' . Yii::app()->user->port . ';dbname=' . $schema . '; charset=utf8'; $this->db = new CDbConnection($connectionString, utf8_decode(Yii::app()->user->name), utf8_decode(Yii::app()->user->password)); $this->db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES \'utf8\''); $this->db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET CHARACTER SET \'utf8\''); $this->db->charset = 'utf8'; $this->db->emulatePrepare = true; $this->db->active = true; // Schema name is set in connection string // $this->db->createCommand('USE ' . $this->db->quoteTableName($schema))->execute(); // Assign to all models which need it ActiveRecord::$db = Routine::$db = Row::$db = Trigger::$db = View::$db = $this->db; // Return connection return $this->db; }
/** * Setup test databases. */ protected function setUp() { $this->executeSqlFile('models/ForeignKeyTest.sql'); Column::$db = ForeignKey::$db = Index::$db = Routine::$db = Row::$db = Schema::$db = Table::$db = Trigger::$db = View::$db = $this->createDbConnection('tabletest'); }
/** * set up the database with function and procedure */ protected function setUp() { $this->executeSqlFile('models/RoutineTest.sql'); Routine::$db = $this->createDbConnection('routinetest'); }