yii2.0增删改查实例讲解
一.创建数据库文件.

创建表
CREATE TABLE `resource` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`texture` varchar(50) NOT NULL COMMENT '材质',
`mark` varchar(50) NOT NULL COMMENT '牌号',
`manufacturers` varchar(100) NOT NULL COMMENT '厂家',
`price` int(11) NOT NULL COMMENT '价格',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
INSERT INTO `resource` VALUES ('1', 'PP', '300H', '中沙石化', '8300');
INSERT INTO `resource` VALUES ('2', 'LDPE', 'LD605', '燕山石化', '9850');
INSERT INTO `resource` VALUES ('3', 'ABS', 'D-190 ', '镇江奇美', '11300');
INSERT INTO `resource` VALUES ('4', 'ABS', 'D-180 ', '镇江奇美', '11500');
INSERT INTO `resource` VALUES ('5', 'ABS', 'D-2400 ', '镇江奇美', '17600');
INSERT INTO `resource` VALUES ('6', 'YH', 'YH100', '测试', '199466');
INSERT INTO `resource` VALUES ('7', 'YH', 'YH200', '测试', '56256');
INSERT INTO `resource` VALUES ('8', 'QW', 'QW-21', '压紧', '19865');
INSERT INTO `resource` VALUES ('9', 'ZX', 'ZX-82', '水果', '98632');
INSERT INTO `resource` VALUES ('10', 'OP', 'OP98', '叶城', '38941');

二.定义控制器controller

namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\data\Pagination;
use app\models\Resources; class ResourcesController extends Controller
{
/**
* 数据列表
*/
public function actionIndex($id = '')
{
$query = Resources::find();
$pages = new Pagination([
'defaultPageSize' => '4',
'totalCount' => $query->count(),
]); $model = $query->orderBy('id DESC')
->offset($pages->offset)
->limit($pages->limit)
->all(); return $this->render('index', [
'model' => $model,
'pages' => $pages,
'id' => $id,
]);
} /**
* 添加数据
*/
public function actionAdd($id = '')
{
if($id){
$model = Resources::findOne($id);
}else{
$model = new Resources();
}
if($model->load(Yii::$app->request->post()) && $model->validate()){
$model->save();
return $this->redirect(array('resources/index'));
}
return $this->render('add', [
'model' => $model,
'id' => $id,
]);
} /**
* 更新数据
*/
public function actionUpdate($id)
{
return $this->redirect(array('resources/add','id' => $id));
} /**
* 删除数据
*/
public function actionDelete($id)
{
Resources::findOne($id)->delete();
return $this->redirect(array('resources/index'));
}
}

三.定义model

namespace app\models;

use Yii;

/**
* This is the model class for table "resource".
*
* @property integer $id
* @property string $texture
* @property string $mark
* @property string $manufacturers
* @property integer $price
*/
class Resources extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'resource';
} /**
* @inheritdoc
*/
public function rules()
{
return [
[['texture', 'mark', 'manufacturers', 'price'], 'required'],
[['price'], 'integer'],
[['texture', 'mark'], 'string', 'max' => 50],
[['manufacturers'], 'string', 'max' => 100],
];
} /**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => "ID",
'texture' =>"材质",
'mark' => Yii::t('app', '牌号'),
'manufacturers' => Yii::t('app', '厂家'),
'price' => Yii::t('app', '价格'),
];
}
}

四.定义视图view
1.index.php

<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>资源列表</title>
<style type="text/css">
.list-head{margin-bottom:10px;overflow:hidden;}
.list-head strong{padding:6px 0;font-size:16px;} .list-table{width:100%;}
.list-table thead{font-weight:bold;}
.list-table td{line-height:30px;text-align:center;border:1px solid #eee;} .fl{float:left;}
.fr{float:right;}
</style>
</head>
<body>
<?= $id ?>
<div class="list-head">
<strong class="fl">资源列表</strong>
<a href="index.php?r=resources/add" class="btn btn-primary fr">添加</a>
</div>
<table class="list-table">
<thead>
<tr>
<td>ID</td>
<td>材质</td>
<td>牌号</td>
<td>厂家</td>
<td>价格</td>
<td>操作</td>
</tr>
</thead>
<tbody>
<?php foreach($model as $resource): ?>
<tr>
<td><?= Html::encode($resource->id) ?></td>
<td><?= Html::encode($resource->texture) ?></td>
<td><?= Html::encode($resource->mark) ?></td>
<td><?= Html::encode($resource->manufacturers) ?></td>
<td><?= Html::encode($resource->price) ?></td>
<td>
<a href="index.php?r=resources/update&id=<?= Html::encode($resource->id) ?>">编辑</a>
<a href="index.php?r=resources/delete&id=<?= Html::encode($resource->id) ?>" onclick="if(confirm('确定删除该条数据?')==false)return false;">删除</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?= LinkPager::widget(['pagination' => $pages]) ?>
</body>
</html>

2.add.php

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?> <?php $form = ActiveForm::begin(); ?>
<?php if($id): ?>
<?= $form->field($model, 'id')->textInput(['value' => $id, 'disabled' => 'disabled']) ?>
<?php endif; ?>
<?= $form->field($model, 'texture') ?>
<?= $form->field($model, 'mark') ?>
<?= $form->field($model, 'manufacturers') ?>
<?= $form->field($model, 'price') ?>
<div class="form-group">
<?php if($id): ?>
<?= Html::submitButton('修改', ['class' => 'btn btn-primary']) ?>
<?php else:?>
<?= Html::submitButton('添加', ['class' => 'btn btn-primary']) ?>
<?php endif; ?>
</div>
<?php ActiveForm::end(); ?>

五.运行结果如下:

yii2.0增删改查实例讲解的更多相关文章

  1. yii2.0增删改查

    //关闭csrf public $enableCsrfValidation = false; 1.sql语句 //查询 $db=\Yii::$app->db ->createCommand ...

  2. python链接oracle数据库以及数据库的增删改查实例

    初次使用python链接oracle,所以想记录下我遇到的问题,便于向我这样初次尝试的朋友能够快速的配置好环境进入开发环节. 1.首先,python链接oracle数据库需要配置好环境. 我的相关环境 ...

  3. java:JSP(JSPWeb.xml的配置,动态和静态导入JSP文件,重定项和请求转发,使用JSP实现数据库的增删改查实例)

    1.JSP的配置: <%@ page language="java" import="java.util.*" pageEncoding="UT ...

  4. EF4.0和EF5.0增删改查的写法区别及执行Sql的方法

    EF4.0和EF5.0增删改查的写法区别 public T AddEntity(T entity) { //EF4.0的写法 添加实体 //db.CreateObjectSet<T>(). ...

  5. MVC ---- EF4.0和EF5.0增删改查的写法区别及执行Sql的方法

    EF4.0和EF5.0增删改查的写法区别 public T AddEntity(T entity) { //EF4.0的写法 添加实体 //db.CreateObjectSet<T>(). ...

  6. python全栈开发中级班全程笔记(第二模块、第三章)(员工信息增删改查作业讲解)

    python全栈开发中级班全程笔记 第三章:员工信息增删改查作业代码 作业要求: 员工增删改查表用代码实现一个简单的员工信息增删改查表需求: 1.支持模糊查询,(1.find name ,age fo ...

  7. 【php增删改查实例】第四节 -自己 DIY 一个数据库管理工具

    本节介绍如何自己DIY一个数据库管理工具,可以在页面输入sql 进行简单的增删改查操作. 首先,找到xampp的安装目录,打开htdocs: 新建一个php文件,名称为 mysqladmin.php ...

  8. YII2生成增删改查

    下载完成后在basic/db.php配置数据库参数. 1.配置虚拟主机后进入YII入口文件 index.php 进行get传值 ?r=gii ,进入创建界面 2.点击 Model Generator下 ...

  9. 百度鹰眼Java接口调用增删改查实例

    因感觉百度鹰眼的使用场景比较符合实际业务,于是对百度鹰眼做了简单功能调试.刚开始使用springframework封装的RestTemplate,但是测试提示ak参数不存在.后又试了几种方法,均提示a ...

随机推荐

  1. 匿名函数 sorted() filter() map() 递归函数

    一. lambda() 匿名函数   说白了,从字面理解匿名函数就是看不见的函数,那么他的看不见表现在哪里呢? 其实就是在查询的时候他们的类型都是lambda的类型所以叫匿名,只要是用匿名函数写的大家 ...

  2. spring4.1.8扩展实战之五:改变bean的定义(BeanFactoryPostProcessor接口)

    本章我们继续实战spring的扩展能力,通过自定义BeanFactoryPostProcessor接口的实现类,来对bean实例做一些控制: 原文地址:https://blog.csdn.net/bo ...

  3. Gitlab仓库搭建和免密使用gitlab

    Gitlab简介 GitLab 是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的web服务. 可通过Web界面进行访问公开的或者私人项目.它拥有与Github类似的 ...

  4. 【MM系列】SAP ABAP 在选择画面显示输出结果

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 在选择画面显示 ...

  5. 校内模拟赛T5:连续的“包含”子串长度( nekameleoni?) —— 线段树单点修改,区间查询 + 尺取法合并

    nekameleoni 区间查询和修改 给定N,K,M(N个整数序列,范围1~K,M次查询或修改) 如果是修改,则输入三个数,第一个数为1代表修改,第二个数为将N个数中第i个数做修改,第三个数为修改成 ...

  6. MySQL 查询语句--------------进阶5:分组查询

    #进阶5:分组查询 /* select 分组函数,列(要求出现在group by的后面) from 表 [where 筛选条件] group by 分组的列表 [order by 子句] 注意: 查询 ...

  7. 工具使用-curl/wget

    curl curl -v www.test.com -H -/MS15- curl -x .xx: http://test.com #使用代理访问 wget wget -e “http_proxy=. ...

  8. [7期]美少妇(msf)和独角兽(unicorn)

    MSF 全称:metasploit-framework    渗透大杀器,黑客工具排名第一(靠前),尤其在内网中.在校期间大学生先跟本校安全部沟通好,一起实验. 得不到校方理解与认可的上SRC或者自己 ...

  9. python函数与方法的区别

    一.函数和方法的区别 1.函数要手动传self,方法不用传 2.如果是一个函数,用类名去调用,如果是一个额方法,用对象去调用 举例说明: class Foo(object): def __init__ ...

  10. Redis进行数据同步

    数据库中的数据一般都涉及到需要对数据进行备份的,这样可以保证数据的安全性,并且如果将一个主设备的数据同步到多个从设备上,允许用户访问数据时可以从多个从设备进行读取, 这样还可以缓解主设备的压力,Red ...