webman:用thinkorm访问数据库(v1.5.7)
一,官方文档地址:
https://www.workerman.net/doc/webman/db/thinkorm.html
二,安装组件
liuhongdi@lhdpc:/data/webman/imageadmin$ composer require -W webman/think-orm
./composer.json has been updated
Running composer update webman/think-orm --with-all-dependencies
Loading composer repositories with package information
Updating dependencies
Lock file operations: 4 installs, 0 updates, 0 removals
- Locking psr/simple-cache (3.0.0)
- Locking topthink/think-helper (v3.1.6)
- Locking topthink/think-orm (v3.0.11)
- Locking webman/think-orm (v1.1.1)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 4 installs, 0 updates, 0 removals
- Downloading webman/think-orm (v1.1.1)
- Installing psr/simple-cache (3.0.0): Extracting archive
- Installing topthink/think-helper (v3.1.6): Extracting archive
- Installing topthink/think-orm (v3.0.11): Extracting archive
- Installing webman/think-orm (v1.1.1): Extracting archive
> support\Plugin::install
> support\Plugin::install
> support\Plugin::install
> support\Plugin::install
Generating autoload files
2 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
No security vulnerability advisories found
Using version ^1.1 for webman/think-orm
三,配置数据库:
config/thinkorm.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<?php return [ 'default' => 'mysql' , 'connections' => [ 'mysql' => [ // 数据库类型 'type' => 'mysql' , // 服务器地址 'hostname' => '127.0.0.1' , // 数据库名 'database' => 'yourbase' , // 数据库用户名 'username' => 'root' , // 数据库密码 'password' => 'yourpassword' , // 数据库连接端口 'hostport' => '3306' , // 数据库连接参数 'params' => [ // 连接超时3秒 \PDO::ATTR_TIMEOUT => 3, ], // 数据库编码默认采用utf8 'charset' => 'utf8mb4' , // 数据库表前缀 'prefix' => '' , // 断线重连 'break_reconnect' => true, // 关闭SQL监听日志 'trigger_sql' => false, // 自定义分页类 'bootstrap' => '' ], ], ]; |
四,访问数据库
1,model/Comment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<?php declare (strict_types = 1); namespace app\model; use think\Model; use think\facade\Db; /** * @mixin \think\Model */ class Comment extends Model { //类名与表名不一致时在这里指定数据表名 protected $table = "it_comment" ; //page:当前页 //size: 每页的数量 public function list( $path ) { $result = Db::table( $this ->table)->where( 'is_mod' ,1)->where( 'path' , $path )->order([ 'cm_id' => 'desc' ])->select(); return $result ; } public function add( $rowIns ) { $result = Db::name( $this ->table)->insertGetId( $rowIns ); return $result ; } } |
2,controller/ImageController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?php namespace app\controller; use support\Request; use app\result\Result; use app\model\Comment as CommentModel; class ImageController { //图片的列表 public function list(Request $request ) { $path = '_image_imageborder' ; $comObj = new CommentModel(); $rows = $comObj ->list( $path ); return Result::Success( $rows ); } } |
说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/08/19/webman-yong-thinkorm-fang-wen-shu-ju-ku-v1-5-7/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
五,测试效果
六,查看webman的版本:
liuhongdi@lhdpc:/data/webman/imageadmin$ composer show workerman/webman-framework
name : workerman/webman-framework
descrip. : High performance HTTP Service Framework.
keywords : High Performance, http service
versions : * v1.5.7
...