037.CI4框架CodeIgniter,使用Model模型绑定数据库表

news/2024/10/5 13:19:03

01、我们创建一个数据库,如下:

CREATE TABLE `user`  (`id` int(20) NOT NULL AUTO_INCREMENT,`username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`userpassword` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`gender` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`details` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`admin` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`group` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`age` int(3) NULL DEFAULT NULL,PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

 02、TestUser.php代码如下:

<?phpnamespace App\Controllers\Test;
use App\Controllers\BaseController;
use CodeIgniter\API\ResponseTrait;//访问地址:http://localhost/phmci4/public/index.php/test/testuser
class TestUser extends BaseController
{use ResponseTrait;protected $userModel;function __construct(){$this->userModel = model('App\Models\Model_user');}//http://localhost/phmci4/public/index.php/test/testuser/indexfunction index(){echo "哈哈!";}//http://localhost/phmci4/public/index.php/test/testuser/testgetuserfunction testgetuser(){$data = $this->userModel->getUser();return $this->respondCreated($data);}//http://localhost/phmci4/public/index.php/test/testuser/testinsertfunction testinsert(){$user = array('username' => 'tianpan','userpassword' => 'guest',//如果没有在$allowedFields中,那么不会被增加和修改'details' => 'good');$data = $this->userModel->insertUser($user);return $this->respondCreated($data);}
}

03、Model_user.php代码如下:

<?phpnamespace App\Models;use CodeIgniter\Model;class Model_user extends Model
{protected $table = 'user';protected $primaryKey = 'id';protected $allowedFields = ['username', 'userpassword'];public function getUser($id = false){if ($id === false) {return $this->findAll();}return $this->where(['id' => $id])->first();}public function insertUser($data){return $this->insert($data);}public function updateUser($data, $id){return $this->update($id, $data);}
}

04、代码结构如下:

 05、访问http://localhost/phmci4/public/index.php/test/testuser/testgetuser,效果如下:

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/54787.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

分享两个方法分析python打包exe

在Python开发中,常常需要将Python脚本打包成独立的可执行文件,以便在没有Python环境的电脑上运行。你是否曾为此感到困惑,不知道该选择哪种工具来实现这一目标?其实,打包Python脚本并不难,关键在于选择合适的工具。今天,我们将介绍两种常用的Python打包工具:PyInstalle…

VMware虚拟机安装的Ubuntu,桥接方式静态ip,内网可以访问,ping外网不可用

1.环境说明 系统:Ubuntu 24.04 环境:VMware下桥接静态IP设置2.问题:ping www.baidu.com报错 [~] ping www.baidu.com ping: www.baidu.com: Temporary failure in name resolution3.解决办法 (1)利用route -n命…

51nod 1204 Parity

闲话 虽然这题好像找不到原题了,但毋庸置疑地说这的确是并查集的好题。 分析 可以先对奇偶区间进行分析,当这个有偶数个 1 时,区间 \(1-(left-1)\) 一定与 区间 \(1-right\) 的奇偶性相同。如此图 \(3-4\) 为偶区间,根据分析,\(1-2\) 为奇区间。\(1-4\) 也为奇区间。但如果…

【信息收集】网络空间测绘FOFA,查询语法最全使用方法(图文解析)

高级搜索基础类别标记类(Special Label)协议类 (type=service)网站类(type=subdomain)证书类地理位置(Location) 时间类(Last update time)独立IP语法(独立IP系列语法,不可和上面其他语法共用)查询基础语法案例html搜索title标题搜索domain域名搜索host主机名搜索o…

Windows10使用MSYS2和VS2019编译FFmpeg详解

本文详解介绍了在windows10下,使用VS2019社区版编译ffmpeg的环境搭建、编译过程,以及期间遇到的各种问题。1 环境准备 1.1 安装Visual Studio 2019 这个步骤相对比较简单,不再详细说明。 1.2 安装msys2 首先需要安装msys2环境以及相关的编译依赖项, 官方网址为: https://ww…

信奥一本通题南沙陈老师解题 1058:求一元二次方程

【题目描述】【输入】输入一行,包含三个浮点数a,b,ca,b,c(它们之间以一个空格分开),分别表示方程ax2+bx+c=0ax2+bx+c=0的系数。【输出】输出一行,表示方程的解。 若两个实根相等,则输出形式为:“x1=x2=...x1=x2=...”; 若两个实根不等,在满足根小者在前的原则,则输出…

PbootCMS使用Ajax无刷新提交留言及表单

留言表单 <form onsubmit="return submsg(this);">联系人<input type="text" name="contacts" required id="contacts">手 机<input type="text" name="mobile" required id="mobile"&…

从代码到产品,我的IT职业成长之路Do

每个人的职业生涯都是一段充满转折和挑战的旅程,当然每一次职业转型都是一次重新定义自己的机会,从2015年开始,当时我刚踏入IT行业,成为一名Java开发者,后来随着时间的推移,我的职业方向逐渐转向了前端开发者,埋头于代码的世界。最终在2018年找到了属于自己的职业定位—…