/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Table::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'status' => $this->status]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'system_name', $this->system_name])->andFilterWhere(['like', 'description', $this->description]); return $dataProvider; }
/** * @param $id * @throws \yii\db\Exception */ public static function createTable($id) { if (($table = Table::findOne((int) $id)) !== null) { if (($fields = Field::findAll(['table_id' => $table->id])) !== null) { $query = ""; foreach ($fields as $item) { $type = $item['fieldType']->type; $query .= "`{$item->system_name}` {$type}" . ($item->length ? "({$item->length})" : "") . " NOT NULL" . ($item->default ? " DEFAULT '{$item->default}'" : "") . " COMMENT '{$item->name}',"; } $query = "CREATE TABLE `{$table->system_name}` (\n `id` INT(11) NOT NULL AUTO_INCREMENT,\n {$query}\n PRIMARY KEY (`id`)\n )\n " . ($table->description ? "COMMENT='{$table->description}'" : "") . "\n COLLATE='utf8_unicode_ci'\n ENGINE=InnoDB\n ;"; Yii::$app->db->createCommand($query)->execute(); } } }
/** * @return \yii\db\ActiveQuery */ public function getTable() { return $this->hasOne(Table::className(), ['id' => 'table_id']); }
?> <?php echo $form->field($model, 'type'); ?> <?php echo $form->field($model, 'length'); ?> <?php echo $form->field($model, 'default'); ?> <?php echo $form->field($model, 'table_id')->dropDownList(\backend\models\Table::getTablesList()); ?> <div class="form-group"> <?php echo Html::submitButton('Search', ['class' => 'btn btn-primary']); ?> <?php echo Html::resetButton('Reset', ['class' => 'btn btn-default']); ?> </div> <?php ActiveForm::end(); ?>
public function actionCreateTable($id) { $model = $this->findModel($id); if ($model->load(Yii::$app->request->post()) && $model->save()) { try { Table::createTable($model->id); } catch (Exception $e) { Yii::$app->getSession()->setFlash('error', $e->getMessage()); return $this->redirect(['update', 'id' => $model->id]); } } return $this->render('createTable', ['model' => $model]); }
/* @var $searchModel backend\models\TableSearch */ /* @var $dataProvider yii\data\ActiveDataProvider */ $this->title = 'Tables'; $this->params['breadcrumbs'][] = $this->title; ?> <div class="table-index"> <h1><?php echo Html::encode($this->title); ?> </h1> <?php // echo $this->render('_search', ['model' => $searchModel]); ?> <p> <?php echo Html::a('Create Table', ['create'], ['class' => 'btn btn-success']); ?> </p> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], 'id', 'name', 'system_name', 'description', ['attribute' => 'status', 'value' => function ($model) { return \backend\models\Table::getStatuses($model->status); }], ['class' => 'yii\\grid\\ActionColumn', 'buttons' => ['list' => function () { return \yii\helpers\Html::a('<span class="glyphicon glyphicon-th-list"></span>', '', ['title' => 'Fields List']); }], 'template' => '{view}{update}{list}{delete}']]]); ?> </div>
use yii\helpers\Html; use yii\widgets\DetailView; /* @var $this yii\web\View */ /* @var $model backend\models\Table */ $this->title = $model->name; $this->params['breadcrumbs'][] = ['label' => 'Tables', 'url' => ['index']]; $this->params['breadcrumbs'][] = $this->title; ?> <div class="table-view"> <h1><?php echo Html::encode($this->title); ?> </h1> <p> <?php echo Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']); ?> <?php echo Html::a('Delete', ['delete', 'id' => $model->id], ['class' => 'btn btn-danger', 'data' => ['confirm' => 'Are you sure you want to delete this item?', 'method' => 'post']]); ?> </p> <?php echo DetailView::widget(['model' => $model, 'attributes' => ['id', 'name', 'system_name', 'description', ['attribute' => 'status', 'value' => \backend\models\Table::getStatuses($model->status)], 'created_at', 'updated_at']]); ?> </div>
<div class="table-form"> <?php $form = ActiveForm::begin(); ?> <?php echo $form->field($model, 'name')->textInput(['maxlength' => true]); ?> <?php echo $form->field($model, 'description')->textArea(['maxlength' => true]); ?> <?php echo $model->isNewRecord ? '' : $form->field($model, 'status')->dropDownList(Table::getStatuses()); ?> <div class="form-group"> <?php echo Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?> </div> <?php ActiveForm::end(); ?> </div>