Пример #1
0
 /**
  * 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;
 }
Пример #2
0
 public function actionSearch()
 {
     $operatorConfig = array('LIKE' => array('needsValue' => true), 'NOT LIKE' => array('needsValue' => true), '=' => array('needsValue' => true), '!=' => array('needsValue' => true), 'REGEXP' => array('needsValue' => "?"), 'NOT REGEXP' => array('needsValue' => "?"), 'IS NULL' => array('needsValue' => false), 'IS NOT NULL' => array('needsValue' => false));
     $operators = array_keys($operatorConfig);
     $config = array_values($operatorConfig);
     Row::$db = $this->db;
     Row::$schema = $this->schema;
     Row::$table = $this->table;
     $row = new Row();
     $commandBuilder = $this->db->getCommandBuilder();
     if (isset($_POST['Row'])) {
         $criteria = new CDbCriteria();
         $i = 0;
         foreach ($_POST['Row'] as $column => $value) {
             $operator = $operators[$_POST['operator'][$column]];
             if (strlen($value) > 0) {
                 $criteria->condition .= ($i > 0 ? ' AND ' : ' ') . $this->db->quoteColumnName($column) . ' ' . $operator . ' ' . $this->db->quoteValue($value);
                 $i++;
             } elseif (isset($_POST['operator'][$column]) && $config[$_POST['operator'][$column]]['needsValue'] === false) {
                 $criteria->condition .= ($i > 0 ? ' AND ' : ' ') . $this->db->quoteColumnName($column) . ' ' . $operator;
                 $i++;
             }
         }
         $query = $this->db->getCommandBuilder()->createFindCommand($this->table, $criteria)->getText();
     } elseif (isset($_POST['query'])) {
         $query = $_POST['query'];
     }
     if (isset($query)) {
         $browsePage = new BrowsePage();
         $browsePage->schema = $this->schema;
         $browsePage->table = $this->table;
         $browsePage->db = $this->db;
         $browsePage->route = 'schema/' . $this->schema . '/tables/' . $this->table . '/browse';
         $browsePage->formTarget = 'schema/' . $this->schema . '/tables/' . $this->table . '/sql';
         $browsePage->execute = true;
         $browsePage->query = $query;
         $browsePage->run();
         $this->render('../global/browse', array('model' => $browsePage));
     } else {
         $this->render('../table/search', array('row' => $row, 'operators' => $operators));
     }
 }
Пример #3
0
 /**
  * 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');
 }