Example #1
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();
         }
     }
 }
Example #2
0
 /**
  * Finds the Table model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Table the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Table::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }