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); }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model=new Column; // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if(isset($_POST['Column'])) { $model->attributes=$_POST['Column']; if($model->save()) $this->redirect(array('view','id'=>$model->column_id)); } $this->render('create',array( 'model'=>$model, )); }
/** * tests to insert a new Column in to the Table */ public function testInsert() { $col = new Column(); $col->TABLE_SCHEMA = 'columntest'; $col->TABLE_NAME = 'test'; $col->COLUMN_NAME = 'testnew'; $col->setDataType('DOUBLE'); $col->COLUMN_DEFAULT = 1; $col->size = 20; $col->scale = 5; $col->setCollation('utf8_general_ci'); $this->assertType('string', $col->save()); // Load column definition $col = Column::model()->findByPk(array('TABLE_SCHEMA' => 'columntest', 'TABLE_NAME' => 'test', 'COLUMN_NAME' => 'testnew')); $this->assertEquals('double', $col->getDataType()); $this->assertEquals(1.0, $col->COLUMN_DEFAULT); $this->assertNull($col->getCollation()); $this->assertEquals(20, $col->size); $this->assertEquals(5, $col->scale); }