Yii2增删改查
Controller

<?php
namespace frontend\controllers;
use frontend\models\User;
use yii\data\Pagination;
class UserController extends \yii\web\Controller
{
//添加的表单页面展示
public function actionIndex()
{
$model = new User();
return $this->render('index',['model'=>$model]);
}
//实现入库
public function actionCreate(){
$model = new User();
$model->status = 1;
$model->create_time = time();
if($model->load(\Yii::$app->request->post()) && $model->validate()){
//密码加密
$model->pwd = $model->setPassword(\Yii::$app->request->post('User[pwd]'));
if($model->save()){
// return '保存成功';
return $this->redirect(['user/lists']);
}else{
// \Yii::$app->getSession()->setFlash('error', '保存失败');
return '保存失败';
}
}else{
return 2;
}
}
//实现列表展示
public function actionLists(){
//创建一个DB来获取所有的用户
$query = User::find();
//得到用户的总数
$count = $query->count();
// 使用总数来创建一个分页对象
$pagination = new Pagination(['totalCount' => $count,'pageSize' => '2']);
//实现分页数据
$users = $query->offset($pagination->offset)
->limit($pagination->limit)
->all();
return $this->render('lists',['data'=>$users,'pagination' => $pagination]);
}
//根据id查询当个信息
public function actionDefault(){
$id = \Yii::$app->request->get("id");
$data = User::findOne($id);
return $this->render('default',['data'=>$data]);
}
//实现根据id进行修改
public function actionUpd(){
//接收参数
$id = \Yii::$app->request->post("id");
$model = User::findOne($id);
if($model->load(\Yii::$app->request->post()) && $model->save()){
return $this->redirect(['user/lists']);
}else{
return '修改失败';
}
}
//实现删除
public function actionDel(){
$id = \Yii::$app->request->get("id");
$model = User::findOne($id);
if($model->delete()){
$this->redirect(['user/lists']);
}else{
return '删除失败';
}
}
}

Model:

<?php
namespace frontend\models;
use Yii;
/**
* This is the model class for table "user".
*
* @property int $id 自增id
* @property string $username 用户名
* @property string $pwd 密码
* @property string $nickname
* @property int $status
* @property int $create_time
*/
class User extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'user';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['username', 'pwd', 'nickname', 'status', 'create_time'], 'required'],
[['status', 'create_time'], 'integer'],
[['username', 'nickname'], 'string', 'max' => 50],
[['pwd'], 'string', 'max' => 255],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => '自增id',
'username' => '账号',
'pwd' => '密码',
'nickname' => '昵称',
'status' => '状态',
'create_time' => '创建时间',
];
}
//密码加密
public function setPassword($password){
return Yii::$app->getSecurity()->generatePasswordHash($password);
}
}

view文件夹下的default.php页面:

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\Url;
?>
<?php $form = ActiveForm::begin([
'action'=>Url::to(['user/upd']),
'method'=>'post'
]) ?>
<?= $form->field($data, 'username') ?>
<?= $form->field($data,'nickname') ?>
<?= $form->field($data,'status')->radioList([1=>'启用',2=>'禁用']) ?>
<?= Html::hiddenInput("id",$data->id) ?>
<?= Html::submitButton('修改', ['class' => 'btn btn-primary']) ?>
<?php ActiveForm::end() ?>

view文件夹下的index.php页面:

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\Url;
?>
<?php $form = ActiveForm::begin([
'action'=>Url::to(['user/create']),
'method'=>'post'
]) ?>
<?= $form->field($model, 'username') ?>
<?= $form->field($model, 'pwd')->passwordInput() ?>
<?= $form->field($model,'nickname') ?>
<?= Html::submitButton('添加', ['class' => 'btn btn-primary']) ?>
<?php ActiveForm::end() ?>

view文件夹下的lists.php页面:

<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
use yii\helpers\Url;
?>
<table border="1">
<tr>
<th>ID</th>
<th>账号</th>
<th>密码</th>
<th>昵称</th>
<th>状态</th>
<th>创建时间</th>
<th>操作</th>
</tr>
<?php foreach ($data as $k => $v): ?>
<tr>
<td><?= Html::encode($v['id']) ?></td>
<td><?= Html::encode($v['username']) ?></td>
<td><?= Html::encode($v['pwd']) ?></td>
<td><?= Html::encode($v['nickname']) ?></td>
<td><?= Html::encode($v['status']==1 ? '启用' : '禁用') ?></td>
<td><?= Html::encode(date("Y-m-d H:i:s",$v['create_time'])) ?></td>
<td>
<a href="<?php echo Url::to(['user/default','id'=>$v['id']]) ?>">修改</a>
<a href="<?php echo Url::to(['user/del','id'=>$v['id']]) ?>">删除</a>
</td>
</tr>
<?php endforeach; ?>
</table>
<?= LinkPager::widget(['pagination' => $pagination]) ?>

数据库:

CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增id', `username` varchar(50) NOT NULL COMMENT '用户名', `pwd` varchar(255) NOT NULL COMMENT '密码', `nickname` varchar(50) NOT NULL, `status` tinyint(4) NOT NULL, `create_time` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=58 DEFAULT CHARSET=utf8;

Yii2增删改查的更多相关文章
- yii2增删改查及AR的理解
yii2增删改查 // 返回 id 为 1 的客户 $customer = Customer::findOne(1); // 返回 id 为 1 且状态为 *active* 的客户 $customer ...
- yii2 增删改查
自己总结的yii2 advanced 版本的简单的增删改查,希望对大家有所帮助 1.gii生成的actionCreate()方法中 获取插入语句的id $id = $model->attribu ...
- 原生YII2 增删改查的一些操作(非ActiveRecord)
1.添加数据 如下,使用insert方法:t_admin_user为数据表名..其他的是属性.. $num = Yii::$app->db->createCommand()->ins ...
- yii2增删改查操作
https://www.yiichina.com/tutorial/996 https://www.yiichina.com/tutorial/834 一.新增 使用model::save()操作进行 ...
- [YII2] 增删改查2
一.新增 使用model::save()操作进行新增数据 $user= new User; $user->username =$username; $user->password =$pa ...
- Yii2.0高级框架数据库增删改查的一些操作(转)
yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ...
- Yii2.0高级框架数据库增删改查的一些操作
yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ...
- YII2.0 数据库增删改查
/*==================== dkhBaseModel 数据库增删改查方法 start ================================*/ //新增一条数据 publ ...
- yii2.0增删改查实例讲解
yii2.0增删改查实例讲解一.创建数据库文件. 创建表 CREATE TABLE `resource` ( `id` int(10) NOT NULL AUTO_INCREMENT, `textur ...
随机推荐
- Illegalmixofcollations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT)foroperation '= 连表查询排序规则问题
两张 表 关联 ,如果 join的字段 排序规则 不一样就会出这个问题 . 解决办法 ,统一 排序规则. 在说说区别,utf8mb4_general_ci 更加快,但是在遇到某些特殊语言或者字符集,排 ...
- c#读sql server数据添加到MySQL数据库
using System;using System.Collections.Generic;using System.Text;using Console = System.Console;using ...
- 1、minimum-depth-of-binary-tree
题目描述 Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the ...
- python之路——12
王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 复习 1.装饰器 开发原则:开放封闭原则 作用:不改变原函数的调用方式,为函数前后扩展功能 本质:闭包函数 ...
- springMVC的高级数据绑定,以及json交互,全局异常配置,
一.窄化请求映射 1.在class上添加@RequestMapping(url)指定通用请求前缀, 限制此类下的所有方法请求url必须以请求前缀开头,通过此方法对url进行分类管理. 如下: @Con ...
- vuex教程,vuex使用介绍案例
1.demopageaction: import Vue from "vue"; import Store from "../../store.js"; imp ...
- 【Selenium】各种方式在选择的时候应该怎么选择
最后再总结一下,各种方式在选择的时候应该怎么选择: 1. 当页面元素有id属性时,最好尽量用id来定位.但由于现实项目中很多程序员其实写的代码并不规范,会缺少很多标准属性,这时就只有选择其他定位方法. ...
- python使用 openpyxl包 excel读取与写入
'''### 写入操作 ###from openpyxl import Workbook#实例化对象wb=Workbook()#创建表ws1=wb.create_sheet('work',0) #默认 ...
- [java,2017-05-15] 内存回收 (流程、时间、对象、相关算法)
内存回收的流程 java的垃圾回收分为三个区域新生代.老年代. 永久代 一个对象实例化时 先去看伊甸园有没有足够的空间:如果有 不进行垃圾回收 ,对象直接在伊甸园存储:如果伊甸园内存已满,会进行一次m ...
- python字符串前面的r/u/b的意义 (笔记)
u/U:表示unicode字符串 : 不是仅仅是针对中文, 可以针对任何的字符串,代表是对字符串进行unicode编码. r/R:非转义的原始字符串: 与普通字符相比,其他相对特殊的字符,其中可能包含 ...