Yii2 中常用的增刪改查操作總結

来源:https://www.cnblogs.com/woods1815/archive/2019/07/07/11145063.html
-Advertisement-
Play Games

一、新增 1、使用save() 2、使用createCommand 原生sql 3、使用createCommand insert 4、批量插入 二、刪除 1、使用delete() 2、使用deleteAll()批量刪除 3、使用createCommand delete() 4、使用createCom ...


 

一、新增

1、使用save()

$model = new User();
$model->name = 'test';
$model->phone = '13000000000';
$model->email = '[email protected]';
$model->save();

2、使用createCommand 原生sql

$sql = "insert into user (name, phone, email) values ('test', '13000000000', '[email protected]')";
Yii::$app->db->createCommand($sql)->execute();

3、使用createCommand insert

Yii::$app->db->createCommand()->insert('user', [
    'name' => 'test',
    'phone' => '13000000000',
    'email' => '[email protected]'
])->execute();

4、批量插入

Yii::$app->db->createCommand()->batchInsert('user', ['name', 'phone', 'email'], [
    ['test1', '13000000000', '[email protected]'],
    ['test2', '13000000001', '[email protected]']
])->execute();

 

二、刪除

1、使用delete()

$user = User::find()->where(['name' => 'test'])->one();
$user->delete();

2、使用deleteAll()批量刪除

User::deleteAll(['name' => 'test']);

3、使用createCommand delete()

Yii::$app->db->createCommand()->delete('user', ['name' => 'test'])->execute();

4、使用createCommand 原生sql

$sql = "delete from user where name = 'test'";
Yii::$app->db->createCommand($sql)->execute();

 

三、更新

1、使用update()

$user = User::find()->where(['name' => 'test'])->one();
$user->phone = '13100000000';
$user->update();  // 或者 $user->save();

2、使用updateAll()

User::updateAll(['phone' => '13100000000'], ['name' => 'test']);

3、使用createCommand update()

Yii::$app->db->createCommand()->update('user', ['phone' => '13100000000'], ['name' => 'test'])->execute();

4、使用createCommand 原生sql

$sql = "update user set phone = '13100000000' where name = 'test'";
Yii::$app->db->createCommand($sql)->execute();

 

四、查詢

1、使用model

// 查詢一條記錄
User::find()->select(['name', 'phone', 'email'])->where(['phone' => '13000000000'])->andWhere(['like', 'name', 'test'])->one();
// 查詢一條記錄數組返回
User::find()->where(['name' => 'test'])->asArray()->one();
// 查詢所有記錄
User::find()->where(['name' => 'test'])->all();
// 查詢所有記錄數組返回
User::find()->where(['name' => 'test'])->asArray()->all();

2、使用createCommand

// 查詢一條記錄
Yii::$app->db->createCommand("select * from user where name = :name and phone = :phone")->bindValues([':name' => 'test', ':phone' => '13000000000'])->queryOne();
// 查詢所有記錄
Yii::$app->db->createCommand("select * from user where name = :name and phone = :phone")->bindValues([':name' => 'test', ':phone' => '13000000000'])->queryAll();

3、使用yii\db\Query()

(new \yii\db\Query())->from('user')->where(['name' => 'test'])->all();

4、子查詢

$subQuery = (new \yii\db\Query())->select(['id'])->from('user')->where(['like', 'name', 'test'])->all();
(new \yii\db\Query())->from('user')->where(['in', 'id', $subQuery])->all();

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 【本文為原創,轉載請註明出處】 技術【CSS+HTML】 佈局【Frameset】 步驟1 Frameset 佈局 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>視頻網站大雜燴</title> <link ...
  • 一、Web前端與後端(Python Web) 1.Web前端:網頁,包含HTML,CSS,JS。 靜態網頁:不能與伺服器交互的網頁 動態網頁:能夠與伺服器交互的網頁 2.Web後端:、Flask,Django、Ajax技術 二、伺服器 1.伺服器:為用戶提供服務的電腦,將數據抽象成URL,以供用戶 ...
  • 【本文為原創,轉載請註明出處】 技術【CSS+HTML】 佈局【Table】 圖片準備【百度圖標、10張不同類型圖】 步驟1 table 佈局 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <titl ...
  • js中打開一個新視窗的方法: 1.window.location.href=“url” 2.jbox.win(); 3.window.open(); js無任何提示的關閉彈出的頁面: window.opener=null; window.open('','_self'); window.close( ...
  • 在安裝本框架的基礎必須安裝合適的Node.js和可以運行的npm包,安裝過程本書前面已經提到過,不再贅述。 ...
  • 有時我們需要統計自定義echarts圖,統計x軸區間的y軸數量。 思路是利用echarts的自定義配置:option.series[i].type='custom'中的renderItem(params, api)函數進行處理,這裡包括了兩個參數:params是對應每一個dataItem的數據信息; ...
  • 首先,在C 中,多態的體現是什麼?虛函數、抽象方法、介面。廢話不多說,直接上代碼: 我們抽象出來基類,將其改造為抽象類和抽象方法,使其在子類中重寫各自的邏輯,而後具體的選擇(也就是條件語句)交給客戶端來處理。 就這麼簡單,細心的我們可以發現,這種多態的寫法好像策略模式,沒錯!設計模式中絕大數的模式都 ...
  • [TOC] 一、I/O模型介紹 為了更好地瞭解I/O模型,我們需要事先回顧下:同步、非同步、阻塞、非阻塞 同步(synchronous) I/O和非同步(asynchronous) I/O,阻塞(blocking) I/O和非阻塞(non blocking)I/O分別是什麼,到底有什麼區別?這個問題其實 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...