/** * Create new table * @param $model \febfeb\dynamicfield\modules\models\Table */ public static function createTable($model) { NodeLogger::sendLog("Create Table"); $db = \Yii::$app->db; $command = $db->createCommand(); $columns = ["id" => "int NOT NULL AUTO_INCREMENT PRIMARY KEY"]; foreach ($model->fields as $field) { $columns[$field->slug_name] = self::convertTypeToDbType($field); } $command->createTable($model->slug_name, $columns)->execute(); NodeLogger::sendLog($columns); }
/** * Creates a new Table model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $modelTable = new Table(); $modelsField = [new Field()]; NodeLogger::sendLog("Init"); if ($modelTable->load(\Yii::$app->request->post())) { NodeLogger::sendLog("Loaded Post Request"); $modelTable->slug_name = PhysicalTableGenerator::getSafeTableName($modelTable->name); $modelsField = Model::createMultiple(Field::classname()); Model::loadMultiple($modelsField, \Yii::$app->request->post()); NodeLogger::sendLog("Loaded Data"); for ($i = 0; $i < count($modelsField); $i++) { $modelsField[$i]->slug_name = PhysicalTableGenerator::getSafeFieldName($modelTable->slug_name, $modelsField[$i]->name); //Util::slugifyToDbSafe($modelsField[$i]->name)."_".\Yii::$app->security->generateRandomString(6); $modelsField[$i]->df_table_id = 1; } NodeLogger::sendLog("Set Relation ID"); // validate all models $valid = $modelTable->validate(); $valid = Model::validateMultiple($modelsField) && $valid; if ($valid) { $transaction = \Yii::$app->db->beginTransaction(); try { if ($flag = $modelTable->save(false)) { foreach ($modelsField as $modelField) { $modelField->df_table_id = $modelTable->id; if (!($flag = $modelField->save(false))) { $transaction->rollBack(); break; } } } if ($flag) { $transaction->commit(); PhysicalTableGenerator::createTable($modelTable); return $this->redirect(['view', 'id' => $modelTable->id]); } } catch (Exception $e) { $transaction->rollBack(); } } } return $this->render('create', ['modelTable' => $modelTable, 'modelsField' => empty($modelsField) ? [new Field()] : $modelsField]); }
<div class="clearfix"></div> <div class="panel panel-default"> <div class="panel-body"> <?php if (isset($dataProvider)) { $column = [['class' => 'yii\\grid\\ActionColumn', 'urlCreator' => function ($action, $model, $key, $index) { // using the column name as key, not mapping to 'id' like the standard generator $params = is_array($key) ? $key : [$model->primaryKey()[0] => (string) $key]; $params[0] = \Yii::$app->controller->id ? \Yii::$app->controller->id . '/' . $action : $action; return Url::toRoute($params); }, 'contentOptions' => ['nowrap' => 'nowrap', 'style' => 'text-align:center']]]; /* @var $searchModel \app\models\search\CobaBroh */ foreach ($searchModel->attributes() as $attribute) { \app\components\NodeLogger::sendLog($attribute); $column[] = $attribute; } ?> <?php echo GridView::widget(['layout' => '{summary}{pager}{items}{pager}', 'dataProvider' => $dataProvider, 'pager' => ['class' => yii\widgets\LinkPager::className(), 'firstPageLabel' => 'First', 'lastPageLabel' => 'Last'], 'filterModel' => $searchModel, 'tableOptions' => ['class' => 'table table-striped table-bordered table-hover'], 'headerRowOptions' => ['class' => 'x'], 'columns' => $column]); ?> <?php } else { echo "Please Generate Model First or Click Here : <br>" . Html::a("<i class='fa fa-check'></i> Generate Model", ["table/generate", "id" => $model->id], ["class" => "btn btn-info"]); } ?> </div> </div> </div>