PHPackages                             thefunpower/db\_medoo - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. thefunpower/db\_medoo

ActiveLibrary[Database &amp; ORM](/categories/database)

thefunpower/db\_medoo
=====================

数据库封装，基于medoo 2.1.10

v2.1.74(1y ago)03045Apache-2.0PHP

Since Jan 10Pushed 1y agoCompare

[ Source](https://github.com/thefunpower/db_medoo)[ Packagist](https://packagist.org/packages/thefunpower/db_medoo)[ RSS](/packages/thefunpower-db-medoo/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)DependenciesVersions (74)Used By (5)

数据库操作
=====

[](#数据库操作)

对 [Medoo](https://github.com/catfan/Medoo) `v2.1.12` 再封装，让操作更简单。

且独立维护`Medoo.php`，不再同步官方代码，如有BUG提交ISSUE

在原类`Medoo.php`上添加

```
$where['FOR UPDATE'] => TRUE
$where['user_name[FIND_IN_SET]']=>(string)10
$where['user_name[RAW]'] => '[a-z0-9]*'

```

安装

```
composer require thefunpower/db_medoo

```

配置

```
/**
* 数据库连接
*/
$medoo_db_config['db_name'] = 'dentalbest_erp';
$medoo_db_config['db_host'] = '127.0.0.1';
$medoo_db_config['db_user'] = 'root';
$medoo_db_config['db_pwd']  = '111111';
$medoo_db_config['db_port'] = 3306;

include __DIR__.'/vendor/thefunpower/db_medoo/boot.php';

```

读库

```
// 从库读库
$medoo_db_config['read_db_host'] = '127.0.0.1';
$medoo_db_config['read_db_name'] = ['read2','read1'];
$medoo_db_config['read_db_user'] = 'root';
$medoo_db_config['read_db_pwd'] = '111111';
$medoo_db_config['read_db_port'] = 3306;

```

在使用只读库时

```
db_active_read();

```

切回默认数据库

```
db_active_default();

```

行锁
--

[](#行锁)

需要放在事务中

```
db_for_update($table,$id);

```

加+
--

[](#加)

```
"age[+]" => 1

```

$where条件
--------

[](#where条件)

```
'user_name[REGEXP]' => '[a-z0-9]*'
'user_name[FIND_IN_SET]'=>(string)10
'user_name[RAW]' => '[a-z0-9]*'

```

```
$where = [
	//like查寻
	'product_num[~]' => 379,
	//等于查寻
	'product_num' => 3669,
	//大于查寻
	'id[>]' => 1,
	'id[>=]' => 1,
	'id[]'=>366,
];
$where['LIMIT'] = 10;
$where['ORDER'] = ['id'=>'DESC'];

```

OR
==

[](#or)

```
//(...  AND ...) OR (...  AND ...)
"OR #1" => [
    "AND #2" => $where,
    "AND #3" => $or_where
    ]
];
//(... OR  ...) AND (...  OR ...)
"AND #1" => [
    "OR #2" => $where,
    "OR #3" => $or_where
    ]
];

```

where字段两个日期之间
-------------

[](#where字段两个日期之间)

字段是datetime类型

```
$date1 = '2022-11-01';
$date2 = '2022-12-14';
db_between_date($field,$date1,$date2)

```

where 两个月份之间
------------

[](#where-两个月份之间)

```
$date1 = '2022-11';
$date2 = '2022-12';
db_between_month($field,$date1,$date2

```

查寻一条记录
------

[](#查寻一条记录)

```
$res = db_get_one("products","*",$where);
$res = db_get_one("products",$where);

```

所有记录
----

[](#所有记录)

```
$res = db_get("products","*",$where);
$res = db_get("products",$where);

```

分页
--

[](#分页)

```
$res  = db_pager("products","*",$where);

```

使用原生方法
------

[](#使用原生方法)

原生方法将不会触发`action`

```
$res = db()->select("products",['id'],[]);

```

查寻某个字段
------

[](#查寻某个字段)

```
$res  = db_get("qr_rule","qr_num",['GROUP'=>'qr_num']);
print_r($res);

```

写入记录
----

[](#写入记录)

```
db_insert($table, $data = [],$don_run_action = false)

```

更新记录
----

[](#更新记录)

```
db_update($table, $data = [], $where = [],$don_run_action = false)

```

取最小值
----

[](#取最小值)

```
db_get_min($table, $join  = "*", $column = null, $where = null)

```

其他一些如取最大值等

```
db_get_max
db_get_count
db_get_has
db_get_rand
db_get_sum
db_get_avg

```

删除
--

[](#删除)

```
db_del($table, $where)

```

action
------

[](#action)

### 写入记录前

[](#写入记录前)

```
do_action("db_insert.$table.before", $data);
do_action("db_save.$table.before", $data);

```

### 写入记录后

[](#写入记录后)

其中`$data`有 `id` 及 `data`

```
do_action("db_insert.$table.after", $action_data);
do_action("db_save.$table.after", $action_data);

```

数据格式

```
$action_data = [];
$action_data['id']    = $id;
$action_data['data']  = $data;

```

### 更新记录前

[](#更新记录前)

```
do_action("db_update.$table.before", $data);
do_action("db_save.$table.before", $data);

```

### 更新记录后

[](#更新记录后)

其中`$data`有 `id` `data` `where`

```
do_action("db_update.$table.after", $action_data);
do_action("db_save.$table.after", $action_data);

```

数据格式

```
$action_data = [];
$action_data['where'] = $where;
$action_data['id']    = $where['id'] ?: '';
$action_data['data']  = $data;

```

```
do_action("db_get_one.$table", $v);

```

删除前
---

[](#删除前)

```
do_action("db_del.$table.before", $where);
do_action("db_del.$table.after", $where);

```

显示所有表名
------

[](#显示所有表名)

```
show_tables($table)

```

取表中字段
-----

[](#取表中字段)

```
get_table_fields($table, $has_key  = true)

```

返回数据库允许的数据，传入其他字段自动忽略
---------------------

[](#返回数据库允许的数据传入其他字段自动忽略)

```
db_allow($table, $data)

```

显示数据库表结构，支持markdown格式
---------------------

[](#显示数据库表结构支持markdown格式)

```
database_tables($name = null, $show_markdown = false)

```

数组排序
----

[](#数组排序)

```
array_order_by($row,$order,SORT_DESC);

```

判断是json数据
---------

[](#判断是json数据)

```
is_json($data)

```

SQL查寻
-----

[](#sql查寻)

```
db_query($sql, $raw = null)
do_action("db_query", $all)

```

其中`$sql`为`select * from table_name where user_id=:user_id`

`$raw` 为 `[':user_id'=>1]`

事务
--

[](#事务)

需要`inner db`支持

```
db_action(function()use($data)){

});

```

id锁
---

[](#id锁)

```
db_for_update($table,$id)

```

设置分页总记录数
--------

[](#设置分页总记录数)

```
db_pager_count($nums = null)

```

连表查寻
----

[](#连表查寻)

```
$data = db_pager("do_order",
["[>
