Beispiel #1
0
 /**
  * Updates an existing Table model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     if ($model->load(Yii::$app->request->post())) {
         if ($model->status == Table::STATUS_ACTIVE) {
             if (($fields = Field::findAll(['table_id' => $model->id])) !== null) {
                 try {
                     Table::createTable($model->id);
                     $modelName = Table::generateModelFromTable($model->system_name);
                     Table::generateCrudFromModel($modelName);
                     Yii::$app->getSession()->setFlash('success', 'Таблица создана, файлы сгенерированы.');
                 } catch (Exception $e) {
                     Yii::$app->getSession()->setFlash('error', $e->getMessage());
                     return $this->redirect(['update', 'id' => $model->id]);
                 }
             } else {
                 Yii::$app->getSession()->setFlash('error', 'Чтобы создать таблицу нужно добавить столбцы.');
                 return $this->redirect(['update', 'id' => $model->id]);
             }
         }
         if ($model->save()) {
             return $this->redirect(['view', 'id' => $model->id]);
         }
     }
     return $this->render('update', ['model' => $model]);
 }
Beispiel #2
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Field::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, 'type' => $this->type, 'length' => $this->length, 'table_id' => $this->table_id]);
     $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'default', $this->default]);
     return $dataProvider;
 }
Beispiel #3
0
 /**
  * @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();
         }
     }
 }
Beispiel #4
0
 /**
  * Finds the Field model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Field the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Field::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }