diff --git a/common/models/Category.php b/common/models/Category.php new file mode 100644 index 0000000..7b5feff --- /dev/null +++ b/common/models/Category.php @@ -0,0 +1,53 @@ + 'ID', + 'cat_name' => 'Cat Name', + 'icon' => 'Icon', + 'icon_type' => 'Icon Type', + 'description' => 'Description', + 'sort_order' => 'Sort Order', + 'created_at' => 'Created At', + 'updated_at' => 'Updated At', + ]; + } + + public function behaviors() { + return [ + TimestampBehavior::className() + ]; + } + +} diff --git a/common/models/CategorySearch.php b/common/models/CategorySearch.php new file mode 100644 index 0000000..4b2e392 --- /dev/null +++ b/common/models/CategorySearch.php @@ -0,0 +1,74 @@ + $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; + } + + // grid filtering conditions + $query->andFilterWhere([ + 'id' => $this->id, + 'icon_type' => $this->icon_type, + 'sort_order' => $this->sort_order, + 'created_at' => $this->created_at, + 'updated_at' => $this->updated_at, + ]); + + $query->andFilterWhere(['like', 'cat_name', $this->cat_name]) + ->andFilterWhere(['like', 'icon', $this->icon]) + ->andFilterWhere(['like', 'description', $this->description]); + + return $dataProvider; + } +} diff --git a/console/controllers/InitController.php b/console/controllers/InitController.php index a296398..ec08ad0 100644 --- a/console/controllers/InitController.php +++ b/console/controllers/InitController.php @@ -8,7 +8,7 @@ use yii\helpers\Console; use yii\db\Query; use yii\helpers\ArrayHelper; use common\models\User; -//use common\models\ars\AdminUser; +use common\models\Category; /** * Description of RbacsetController @@ -19,6 +19,17 @@ class InitController extends Controller { public function actionIndex() { echo "index\n"; + Category::deleteAll(); + for ($i = 0; $i < 100; $i++) { + $model = new Category(); + $model->attributes = [ + "cat_name" => "这是一个测试分类{$i}", + "icon" => 'fa', + "icon_type" => 1, + "sort_order" => $i + ]; + $model->save(); + } } public function actionCreateAdmin($password = NULL) { @@ -83,7 +94,7 @@ class InitController extends Controller { if ($model->save()) { //$oRole = $auth->getRole("超级管理员"); //if ($auth->assign($oRole, $model->id)) { - $this->stdout("Create admin success!\n", Console::FG_GREEN); + $this->stdout("Create admin success!\n", Console::FG_GREEN); //} } } diff --git a/console/migrations/m190802_072830_add_category.php b/console/migrations/m190802_072830_add_category.php index a76f38f..05dad5e 100644 --- a/console/migrations/m190802_072830_add_category.php +++ b/console/migrations/m190802_072830_add_category.php @@ -5,38 +5,20 @@ use yii\db\Migration; /** * Class m190802_072830_add_category */ -class m190802_072830_add_category extends Migration -{ - /** - * {@inheritdoc} - */ - public function safeUp() - { - - } +class m190802_072830_add_category extends Migration { /** * {@inheritdoc} */ - public function safeDown() - { - echo "m190802_072830_add_category cannot be reverted.\n"; - - return false; + public function up() { + $sql = file_get_contents(__DIR__ . "/sql/add_category.sql"); + $this->execute($sql); } - /* - // Use up()/down() to run migration code without a transaction. - public function up() - { + public function down() { + $this->dropTable("category"); + return true; } - public function down() - { - echo "m190802_072830_add_category cannot be reverted.\n"; - - return false; - } - */ } diff --git a/console/migrations/sql/add_category.sql b/console/migrations/sql/add_category.sql new file mode 100644 index 0000000..e801049 --- /dev/null +++ b/console/migrations/sql/add_category.sql @@ -0,0 +1,11 @@ +DROP TABLE IF EXISTS `category`; +CREATE TABLE `category` ( + `id` int(11) AUTO_INCREMENT PRIMARY KEY, + `cat_name` varchar(64) NOT NULL, + `icon` varchar(64) DEFAULT NULL, + `icon_type` tinyint(1) NOT NULL DEFAULT 1, + `description` text NOT NULL DEFAULT '', + `sort_order` smallint(3) NOT NULL DEFAULT 100, + `created_at` int(11) NOT NULL DEFAULT 0, + `updated_at` int(11) NOT NULL DEFAULT 0 +)ENGINE=INNODB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; diff --git a/kcadmin/assets/AppAsset.php b/kcadmin/assets/AppAsset.php index 63b8e8d..7c22300 100644 --- a/kcadmin/assets/AppAsset.php +++ b/kcadmin/assets/AppAsset.php @@ -20,6 +20,7 @@ class AppAsset extends AssetBundle 'yii\web\YiiAsset', 'yii\bootstrap\BootstrapAsset', 'yii\bootstrap\BootstrapPluginAsset', - 'blobt\web\AdminlteAsset' + 'blobt\web\AdminlteAsset', + 'blobt\web\DatatableBootstrap' ]; } diff --git a/kcadmin/controllers/CategoryController.php b/kcadmin/controllers/CategoryController.php new file mode 100644 index 0000000..0bbc472 --- /dev/null +++ b/kcadmin/controllers/CategoryController.php @@ -0,0 +1,127 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ]; + } + + /** + * Lists all Category models. + * @return mixed + */ + public function actionIndex() + { + $searchModel = new CategorySearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single Category model. + * @param integer $id + * @return mixed + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new Category model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + public function actionCreate() + { + $model = new Category(); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } + + return $this->render('create', [ + 'model' => $model, + ]); + } + + /** + * Updates an existing Category model. + * If update is successful, the browser will be redirected to the 'view' page. + * @param integer $id + * @return mixed + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionUpdate($id) + { + $model = $this->findModel($id); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } + + return $this->render('update', [ + 'model' => $model, + ]); + } + + /** + * Deletes an existing Category model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param integer $id + * @return mixed + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionDelete($id) + { + $this->findModel($id)->delete(); + + return $this->redirect(['index']); + } + + /** + * Finds the Category model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param integer $id + * @return Category the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = Category::findOne($id)) !== null) { + return $model; + } + + throw new NotFoundHttpException('The requested page does not exist.'); + } +} diff --git a/kcadmin/views/category/_form.php b/kcadmin/views/category/_form.php new file mode 100644 index 0000000..a26664e --- /dev/null +++ b/kcadmin/views/category/_form.php @@ -0,0 +1,35 @@ + + +
+ = Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?> + = Html::a('Delete', ['delete', 'id' => $model->id], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => 'Are you sure you want to delete this item?', + 'method' => 'post', + ], + ]) ?> +
+ + = DetailView::widget([ + 'model' => $model, + 'attributes' => [ + 'id', + 'cat_name', + 'icon', + 'icon_type', + 'description:ntext', + 'sort_order', + 'created_at', + 'updated_at', + ], + ]) ?> + +