99国产在线视频有精品视频_国产精品久久久久久亚洲影视 _国产三级日本三级韩国三级在线看_精精国产XXXX视频在线my_乱VODAFONEWIFI熟妇_超碰cao国产精品一区二区_91久久精品青青草原伊人_欧美,一区,日韩,精品

YZMPHP V2.9 數據庫操作DB方法更新

1326次瀏覽 更新日期:2024-03-12 23:45:52 分類:程序交流 評論:6

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')


我來說兩句
  • yzmcms
    yzmcms 官網認證 1年前
    YzmCMS V7.1基于YZMPHP V2.9開發,并將這周發布新版本
    2
    回復
  • sxsmlj
    sxsmlj 1年前
    0
    回復
  • desen
    desen 1年前
    0
    回復
  • kinji
    kinji 1年前
    期待
    0
    回復
  • lys
    lys 1年前
    好呀!YzmCMS的更新會給我們帶來更好的感覺!
    0
    回復
  • 四鬼
    四鬼 1年前
    0
    回復
作者信息
發布見解
發內容 回頂部