YII2 CURD 增删改查
来源:互联网 发布:h3c交换机端口up down 编辑:程序博客网 时间:2024/05/22 12:33
backend\controllers\PostController.php
<?phpnamespace backend\controllers;use Yii;use common\models\Post;use common\models\search\PostSearch;use yii\web\Controller;use yii\web\NotFoundHttpException;use yii\filters\VerbFilter;use frontend\base\BaseFrontController;use backend\base\BaseBackController;/** * PostController implements the CRUD actions for Post model. */class PostController extends BaseBackController{ public function behaviors() { return [ 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'delete' => ['post'], ], ], ]; } /** * Lists all Post models. * @return mixed */ public function actionIndex() { $searchModel = new PostSearch; $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams()); return $this->render('index', [ 'dataProvider' => $dataProvider, 'searchModel' => $searchModel, ]); } /** * Displays a single Post model. * @param integer $id * @return mixed */ public function actionView($id) { return $this->render('view', [ 'model' => $this->findModel($id), ]); } /** * Creates a new Post model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Post; if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', [ 'model' => $model, ]); } } /** * Updates an existing Post 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()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', [ 'model' => $model, ]); } } /** * Deletes an existing Post model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { $this->findModel($id)->delete(); return $this->redirect(['index']); } /** * Finds the Post model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Post the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Post::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }}
backend\models\Post.php
<?phpnamespace common\models;use Yii;use base\BaseActiveRecord;/** * This is the model class for table "post". * * @property integer $id * @property integer $thread_id * @property integer $user_id * @property string $user_name * @property string $title * @property string $body * @property string $create_time * @property string $modify_time * @property integer $supports * @property integer $againsts * @property integer $floor * @property string $note */class Post extends BaseActiveRecord{/** * @inheritdoc */public static function tableName(){return 'post';}/** * @inheritdoc */public function rules(){return [[['thread_id','user_id','user_name','body','create_time'],'required'],[['thread_id','user_id','supports','againsts','floor'],'integer'],[['body'],'string'],[['create_time','modify_time'],'safe'],[['user_name'],'string','max' => 32],[['title'],'string','max' => 128],[['note'],'string','max' => 64]];}/** * @inheritdoc */public function attributeLabels(){return ['id' => 'ID','thread_id' => 'Thread ID','user_id' => 'User ID','user_name' => 'User Name','title' => '标题','body' => '内容','create_time' => 'Create Time','modify_time' => 'Modify Time','supports' => '支持数','againsts' => '反对数','floor' => '楼层','note' => 'Note'];}}
backend\views\post\index.php
<?phpuse yii\helpers\Html;use yii\grid\GridView;/** * @var yii\web\View $this * @var yii\data\ActiveDataProvider $dataProvider * @var common\models\search\PostSearch $searchModel */$this->title = 'Posts';$this->params['breadcrumbs'][] = $this->title;?><div class="post-index"> <h1><?= Html::encode($this->title) ?></h1> <?php // echo $this->render('_search', ['model' => $searchModel]); ?> <p> <?= Html::a('Create Post', ['create'], ['class' => 'btn btn-success']) ?> </p> <?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ ['class' => 'yii\grid\SerialColumn'], 'id', 'thread_id', 'user_id', 'user_name', 'title', // 'body:ntext', // 'create_time', // 'modify_time', // 'supports', // 'againsts', // 'floor', // 'note', ['class' => 'yii\grid\ActionColumn'], ], ]); ?></div>
backend\views\post\create.php
<?phpuse yii\helpers\Html;/** * @var yii\web\View $this * @var common\models\Post $model */$this->title = 'Create Post';$this->params['breadcrumbs'][] = ['label' => 'Posts', 'url' => ['index']];$this->params['breadcrumbs'][] = $this->title;?><div class="post-create"> <h1><?= Html::encode($this->title) ?></h1> <?= $this->render('_form', [ 'model' => $model, ]) ?></div>
backend\views\post\update.php
<?phpuse yii\helpers\Html;/** * @var yii\web\View $this * @var common\models\Post $model */$this->title = 'Update Post: ' . $model->title;$this->params['breadcrumbs'][] = ['label' => 'Posts', 'url' => ['index']];$this->params['breadcrumbs'][] = ['label' => $model->title, 'url' => ['view', 'id' => $model->id]];$this->params['breadcrumbs'][] = 'Update';?><div class="post-update"> <h1><?= Html::encode($this->title) ?></h1> <?= $this->render('_form', [ 'model' => $model, ]) ?></div>
backend\views\post\view.php
<?phpuse yii\helpers\Html;use yii\widgets\DetailView;/** * @var yii\web\View $this * @var common\models\Post $model */$this->title = $model->title;$this->params['breadcrumbs'][] = ['label' => 'Posts', 'url' => ['index']];$this->params['breadcrumbs'][] = $this->title;?><div class="post-view"> <h1><?= Html::encode($this->title) ?></h1> <p> <?= 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', ], ]) ?> </p> <?= DetailView::widget([ 'model' => $model, 'attributes' => [ 'id', 'thread_id', 'user_id', 'user_name', 'title', 'body:ntext', 'create_time', 'modify_time', 'supports', 'againsts', 'floor', 'note', ], ]) ?></div>
backend\views\post\_form.php
<?phpuse yii\helpers\Html;use yii\widgets\ActiveForm;/** * @var yii\web\View $this * @var common\models\Post $model * @var yii\widgets\ActiveForm $form */?><div class="post-form"> <?php $form = ActiveForm::begin(); ?> <?= $form->field($model, 'thread_id')->textInput() ?> <?= $form->field($model, 'user_id')->textInput() ?> <?= $form->field($model, 'user_name')->textInput(['maxlength' => 32]) ?> <?= $form->field($model, 'title')->textInput(['maxlength' => 128]) ?> <?= $form->field($model, 'body')->textarea(['rows' => 6]) ?> <?= $form->field($model, 'create_time')->textInput() ?> <?= $form->field($model, 'supports')->textInput() ?> <?= $form->field($model, 'againsts')->textInput() ?> <?= $form->field($model, 'floor')->textInput() ?> <?= $form->field($model, 'modify_time')->textInput() ?> <?= $form->field($model, 'note')->textInput(['maxlength' => 64]) ?> <div class="form-group"> <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> </div> <?php ActiveForm::end(); ?></div>backend\views\post\_search.php
<?phpuse yii\helpers\Html;use yii\widgets\ActiveForm;/** * @var yii\web\View $this * @var common\models\search\PostSearch $model * @var yii\widgets\ActiveForm $form */?><div class="post-search"> <?php $form = ActiveForm::begin([ 'action' => ['index'], 'method' => 'get', ]); ?> <?= $form->field($model, 'id') ?> <?= $form->field($model, 'thread_id') ?> <?= $form->field($model, 'user_id') ?> <?= $form->field($model, 'user_name') ?> <?= $form->field($model, 'title') ?> <?php // echo $form->field($model, 'body') ?> <?php // echo $form->field($model, 'create_time') ?> <?php // echo $form->field($model, 'modify_time') ?> <?php // echo $form->field($model, 'supports') ?> <?php // echo $form->field($model, 'againsts') ?> <?php // echo $form->field($model, 'floor') ?> <?php // echo $form->field($model, 'note') ?> <div class="form-group"> <?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?> <?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?> </div> <?php ActiveForm::end(); ?></div>
1 0
- YII2 CURD 增删改查
- CURD增删改查
- yii2 增删改查
- yii2增删改查
- YII2增删改查
- Yii 增删改查(CURD)
- CURD-增删查改操作
- Yii2.0 增删改查
- Yii2.0增删改查
- Yii2.0 增删改查
- yii2.0 增删改查
- Yii2.0增删改查
- ThinkPHP实例教程--CURD (增删改查)
- Hibernate实现增删查改CURD例子
- 增删改查CURD(数据库助手)
- Hibernate---curd(增删查改)操作
- 2、Mybatis增删改查(CURD)
- Yii2.0 数据库的增删改查
- 证书签名
- EXTJS 详细学习资料
- Android开发——数据库内容的查询
- 线程和进程的区别是什么?
- 解决OOM的方法
- YII2 CURD 增删改查
- (4-3)查看fsimage和edits
- Makefile 中:= ?= += =的区别
- 1.11
- BADI修改VL02N屏幕字段
- Android开发——sd卡的使用
- 很有用的android优化技巧
- 【2】KMP算法的一些理解问题
- Sonatype Nexus 搭建Maven 私服