YII2 CURD 增删改查
backendcontrollersPostController.php
<?php
namespace backendcontrollers;
use Yii;
use commonmodelsPost;
use commonmodelssearchPostSearch;
use yiiwebController;
use yiiwebNotFoundHttpException;
use yiifiltersVerbFilter;
use frontendaseBaseFrontController;
use backendaseBaseBackController;
/**
* 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.");
}
}
}
backendmodelsPost.php
<?php
namespace commonmodels;
use Yii;
use baseBaseActiveRecord;
/**
* 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"
];
}
}backendviewspostindex.php
<?php
use yiihelpersHtml;
use yiigridGridView;
/**
* @var yiiwebView $this
* @var yiidataActiveDataProvider $dataProvider
* @var commonmodelssearchPostSearch $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" => "yiigridSerialColumn"],
"id",
"thread_id",
"user_id",
"user_name",
"title",
// "body:ntext",
// "create_time",
// "modify_time",
// "supports",
// "againsts",
// "floor",
// "note",
["class" => "yiigridActionColumn"],
],
]); ?>
</div>backendviewspostcreate.php
<?php
use yiihelpersHtml;
/**
* @var yiiwebView $this
* @var commonmodelsPost $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>backendviewspostupdate.php
<?php
use yiihelpersHtml;
/**
* @var yiiwebView $this
* @var commonmodelsPost $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>backendviewspostview.php
<?php
use yiihelpersHtml;
use yiiwidgetsDetailView;
/**
* @var yiiwebView $this
* @var commonmodelsPost $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>
backendviewspost\_form.php
<?php
use yiihelpersHtml;
use yiiwidgetsActiveForm;
/**
* @var yiiwebView $this
* @var commonmodelsPost $model
* @var yiiwidgetsActiveForm $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>
backendviewspost\_search.php
<?php
use yiihelpersHtml;
use yiiwidgetsActiveForm;
/**
* @var yiiwebView $this
* @var commonmodelssearchPostSearch $model
* @var yiiwidgetsActiveForm $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>声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇: Yii2 多表联合
- 下一篇: Yii2 自定义class、自定义全局函数
