Example #1
0
 public function actionCreate()
 {
     $column = new Column();
     $column->TABLE_NAME = $this->table;
     $table = Table::model()->findByPk(array('TABLE_SCHEMA' => $this->schema, 'TABLE_NAME' => $this->table));
     if (isset($_POST['Column'])) {
         $column->attributes = $_POST['Column'];
         /*
          * Add index
          */
         $addIndices = array();
         if (isset($_POST['createIndexPrimary'])) {
             $column->createPrimaryKey = true;
         }
         if (isset($_POST['createIndex'])) {
             $addIndices['INDEX'] = $column->COLUMN_NAME;
         }
         if (isset($_POST['createIndexUnique'])) {
             $column->createUniqueKey = true;
         }
         if (isset($_POST['createIndexFulltext'])) {
             $addIndices['FULLTEXT'] = $column->COLUMN_NAME . (array_search($column->COLUMN_NAME, $addIndices) !== false ? '_fulltext' : '');
         }
         if ($sql = $column->save()) {
             $response = new AjaxResponse();
             $response->addNotification('success', Yii::t('core', 'successAddColumn', array('{col}' => $column->COLUMN_NAME)), null, $sql);
             $response->refresh = true;
             foreach ($addIndices as $type => $indexName) {
                 try {
                     $index = new Index();
                     $index->throwExceptions = true;
                     $index->TABLE_NAME = $this->table;
                     $index->TABLE_SCHEMA = $this->schema;
                     $index->INDEX_NAME = $indexName;
                     $index->setType($type);
                     $indexCol = new IndexColumn();
                     $indexCol->COLUMN_NAME = $column->COLUMN_NAME;
                     $index->columns = array($indexCol);
                     $sql = $index->save();
                     $response->addNotification('success', Yii::t('core', 'successCreateIndex', array('{index}' => $index->INDEX_NAME)), null, $sql);
                 } catch (DbException $ex) {
                     $response->addNotification('error', Yii::t('core', 'errorCreateIndex', array('{index}' => $index->INDEX_NAME)), $ex->getText(), $ex->getSql());
                 }
             }
             $this->sendJSON($response);
         }
     }
     $collations = Collation::model()->findAll(array('order' => 'COLLATION_NAME', 'select' => 'COLLATION_NAME, CHARACTER_SET_NAME AS collationGroup'));
     CHtml::generateRandomIdPrefix();
     $data = array('column' => $column, 'table' => $table, 'collations' => $collations);
     $data['formBody'] = $this->renderPartial('formBody', $data, true);
     $this->render('form', $data);
 }
Example #2
0
 public function actionCreate()
 {
     $this->layout = false;
     $table = new Table();
     $column = new Column();
     if (isset($_POST['Table'], $_POST['Column'])) {
         $table->attributes = $_POST['Table'];
         $column->attributes = $_POST['Column'];
         /*
          * Add index
          */
         $addIndices = array();
         if (isset($_POST['createIndexPrimary'])) {
             $column->createPrimaryKey = true;
         }
         if (isset($_POST['createIndex'])) {
             $addIndices['INDEX'] = $column->COLUMN_NAME;
         }
         if (isset($_POST['createIndexUnique'])) {
             $column->createUniqueKey = true;
         }
         if (isset($_POST['createIndexFulltext'])) {
             $addIndices['FULLTEXT'] = $column->COLUMN_NAME . (array_search($column->COLUMN_NAME, $addIndices) !== false ? '_fulltext' : '');
         }
         $table->columns = array($column);
         if ($sql = $table->insert()) {
             $response = new AjaxResponse();
             $response->addNotification('success', Yii::t('core', 'successAddTable', array('{table}' => $table->TABLE_NAME)), null, $sql);
             $response->redirectUrl = '#tables/' . $table->TABLE_NAME . '/structure';
             $response->executeJavaScript('sideBar.loadTables(schema);');
             foreach ($addIndices as $type => $indexName) {
                 try {
                     $index = new Index();
                     $index->throwExceptions = true;
                     $index->TABLE_NAME = $table->TABLE_NAME;
                     $index->TABLE_SCHEMA = $this->schema;
                     $index->INDEX_NAME = $indexName;
                     $index->setType($type);
                     $indexCol = new IndexColumn();
                     $indexCol->COLUMN_NAME = $column->COLUMN_NAME;
                     $index->columns = array($indexCol);
                     $sql = $index->save();
                     $response->addNotification('success', Yii::t('core', 'successCreateIndex', array('{index}' => $index->INDEX_NAME)), null, $sql);
                     $response->refresh = true;
                 } catch (DbException $ex) {
                     $response->addNotification('error', Yii::t('core', 'errorCreateIndex', array('{index}' => $index->INDEX_NAME)), $ex->getText(), $ex->getSql());
                 }
             }
             $this->sendJSON($response);
         }
     }
     $collations = Collation::model()->findAll(array('order' => 'COLLATION_NAME', 'select' => 'COLLATION_NAME, CHARACTER_SET_NAME AS collationGroup'));
     CHtml::generateRandomIdPrefix();
     $data = array('table' => $table, 'column' => $column, 'collations' => $collations, 'storageEngines' => StorageEngine::getSupportedEngines());
     $data['columnForm'] = $this->renderPartial('/column/formBody', $data, true);
     $this->render('form', $data);
 }
Example #3
0
 /**
  * Tests to add an Index to a Column with Type Fulltext
  */
 public function testSetTypeFullText()
 {
     // Create index
     $index = new Index();
     $index->TABLE_NAME = 'table2';
     $index->TABLE_SCHEMA = 'indextest';
     $index->INDEX_NAME = 'newname';
     $index->setType('FULLTEXT');
     // Add new column
     $columns = $index->columns;
     $col = new IndexColumn();
     $col->COLUMN_NAME = 'pk';
     $columns[] = $col;
     $col = new IndexColumn();
     $col->COLUMN_NAME = 'varchar';
     $col->SUB_PART = 10;
     $columns[] = $col;
     $index->columns = $columns;
     // Try saving
     $index->save();
     // Reload index and load index columns
     $index->refresh();
     $cols = IndexColumn::model()->findAllByAttributes(array('TABLE_SCHEMA' => $index->TABLE_SCHEMA, 'TABLE_NAME' => $index->TABLE_NAME, 'INDEX_NAME' => $index->INDEX_NAME));
     // Check properties
     $this->assertEquals('newname', $index->INDEX_NAME);
     $this->assertEquals('FULLTEXT', $index->getType());
 }
Example #4
0
 public function actionUpdate()
 {
     $index = Index::model()->findByAttributes(array('TABLE_SCHEMA' => $this->schema, 'TABLE_NAME' => $this->table, 'INDEX_NAME' => $this->index));
     if ($index == null) {
         $index = new Index();
         $index->TABLE_SCHEMA = $this->schema;
         $index->TABLE_NAME = $this->table;
         $index->INDEX_NAME = $this->index;
     }
     $table = Table::model()->findByPk(array('TABLE_SCHEMA' => $this->schema, 'TABLE_NAME' => $this->table));
     if (isset($_POST['Index'])) {
         $index->attributes = $_POST['Index'];
         $index->columns = $this->getColumnsFromRequest();
         if ($sql = $index->save()) {
             $response = new AjaxResponse();
             $response->addNotification('success', Yii::t('core', 'successAlterIndex', array('{index}' => $index->INDEX_NAME)), null, $sql);
             $response->refresh = true;
             $this->sendJSON($response);
         }
     }
     $indexTypes = $table->getSupportedIndexTypes();
     $this->render('form', array('index' => $index, 'indexTypes' => $indexTypes, 'addColumnData' => $this->getColumnSelect($table)));
 }