YZMCMS内容管理系统

首页 > 动态

YZMPHP V2.9 数据库操作DB方法更新

来源:原创    2024-03-12 23:58:31   编辑:管理员   浏览:244

YZMPHP V2.9 发布了,框架更新优化了很多细节问题,本文主要介绍更新的DB操作类 wheres 方法的使用,并介绍框架的新版本中如何查询数据及使用,旧版本的文档手册请转至 https://www.yzmcms.com/dongtai/37.html


wheres 方法是 where 的升级版,wheres 方法是 YZMPHP v2.9 新增的方法,可以完成包括普通查询、表达式查询、快捷查询、区间查询、组合查询在内的查询操作,wheres 方法的参数支持字符串和数组,我们建议为数组查询。


一、使用说明:

1.字符串条件:

$db->wheres('status=1 and age>18')->select();

2.数组条件:

// 以下为YZMPHP V2.9 新增的 wheres 条件语法,旧版链接:https://www.yzmcms.com/dongtai/37.html
$where['字段']  = 数组;
$where['字段1']  = array('表达式','字段条件1');
$where['字段2']  = array('表达式','字段条件2');
$where['字段3']  = array('表达式','字段条件3','可选参数(回调函数)');

2.1第一个参数【表达式】

表达式含义(不分大小写):
eq  等于(=)
neq 不等于(<>)
gt  大于(>)
egt 大于等于(>=)
lt  小于(<)
elt 小于等于(<=)
like    模糊查询
notlike (不在)模糊查询
[not] in    (不在)in 查询
[not] between   (不在)区间查询

其中,当“表达式”为等于(eq)时,可以省略不写,即:

$where['name']  = array('小明');
等价于
$where['name']  = array('eq','小明');


2.2第二个参数【查询条件】

查询条件可为字符串或数组,例如:

$where['cms'] = ['neq', 'yzmcms'];
$where['id'] = ['in', ['11','22','33']];
$db->wheres($where)->select();


2.3第三个参数【回调函数】

该参数是可选参数,传入为函数名称,将传入的“回调函数”应用到“字段条件”每个元素之上,例如:

$where['cms'] = ['neq', 'yzmcms', 'trim'];
$where['id'] = ['in', ['11','22','33'], 'intval'];
$db->wheres($where)->select();


3.wheres 方法的条件或查询(OR)

$db->wheres($where1, $where2, $where3)->select();


二、wheres 与 where 的区别:

1. where 方法的条件字段值只能是数字或字符串,wheres 方法的条件字段值只能是数组。

// where 方式:
$where['name'] = '小明';

// wheres 方式:
$wheres['name'] = ['小明'];
$wheres['name'] = ['eq', '小明'];


2. wheres 方法是 where 的升级版,支持更加复杂的语法查询。

下面我们举一些例子来说明:

$wheres = $where = [];

$wheres['cms'] = ['eq', 'yzmcms'];
// 等同于 $where['cms'] = 'yzmcms';

$wheres['cms'] = ['neq', 'yzmcms'];
// 等同于 $where['cms!='] = 'yzmcms';

$wheres['age'] = ['gt', '18'];
// 等同于 $where['age>'] = '18';

$wheres['age'] = ['egt', '18'];
// 等同于 $where['age>='] = '18';

$wheres['age'] = ['lt', '18'];
// 等同于 $where['age<'] = '18';

$wheres['age'] = ['elt', '18'];
// 等同于 $where['age<='] = '18';

$wheres['cms'] = ['like', '%yzmcms%'];
// 等同于 $where['cms'] = '%yzmcms%';

// wheres 比 where 多支持的语法查询:
$wheres['cms'] = ['notlike', '%yzmcms%'];

$wheres['id'] = ['in', ['11','22','33']];
$wheres['id'] = ['in', ['11','22','33'], 'intval'];

$wheres['id'] = ['notin', ['11','22','33']];
$wheres['id'] = ['notin', ['11','22','33'], 'intval'];

$wheres['id'] = ['between', ['1','99']];
$wheres['id'] = ['between', ['1','99'], 'intval'];

$wheres['id'] = ['notbetween', ['1','99']];
$wheres['id'] = ['notbetween', ['1','99'], 'intval'];


// 执行查询操作并打印结果
$db->wheres($wheres)->select();
P($res);

// 打印生成的SQL
$db->lastsql();


3. 当 where 和 wheres 同时使用时,因为 wheres 的优先级更高,程序只会解析 wheres 条件表达式。

$wheres = [
    'name' => ['like','小明'],
    'age' => ['egt','88']
];
$where = [
    'id>' => 30
];

// 无论wheres和where的先后顺序如何,都只解析 wheres
$db->wheres($wheres)->where($where)->select();
// $db->where($where)->wheres($wheres)->select();

// 生成的SQL:
// SELECT * FROM `yzmcms` . `yzm_aaa` WHERE (name LIKE '小明' AND age >= '88')

// 使用delete方法时,尽管使用了where传参,依然wheres优先级更高
$db->wheres($wheres)->delete($where);

// 生成的SQL:
// DELETE FROM `yzmcms` . `yzm_aaa` WHERE (name LIKE '小明' AND age >= '88')

// 使用update方法时,尽管使用了where传参,依然wheres优先级更高
$db->wheres($wheres)->update([
        'name'=>88
    ], $where);

// 生成的SQL:
// UPDATE `yzmcms` . `yzm_aaa` SET `name` = '88' WHERE (name LIKE '小明' AND age >= '88')


4. wheres 表达式应用于链式操作时,update和delete方法的where条件可以省略。

$wheres = [
    'name' => ['like','小明'],
    'age' => ['egt','88']
];

$db->wheres($wheres)->delete();

// 生成的SQL:
// DELETE FROM `yzmcms` . `yzm_aaa` WHERE (name LIKE '小明' AND age >= '88')

$db->wheres($wheres)->update([
        'name'=>88
    ]);

// 生成的SQL:
// UPDATE `yzmcms` . `yzm_aaa` SET `name` = '88' WHERE (name LIKE '小明' AND age >= '88')