條件查詢
$customers = Customer::find()->where($cond)->all();
$cond就是我們所謂的條件,條件的寫法也根據查詢資料的不同存在差異,那麼如何用yii2的方式來寫查詢條件呢?
[[簡單條件]]
// SQL: (type = 1) AND (status = 2). $cond = ['type' => 1, 'status' => 2] // SQL:(id IN (1, 2, 3)) AND (status = 2) $cond = ['id' => [1, 2, 3], 'status' => 2] //SQL:status IS NULL $cond = ['status' => null]
【and】:將不同的條件組合在一起,用法舉例:
//SQL:`id=1 AND id=2` $cond = ['and', 'id=1', 'id=2'] //SQL:`type=1 AND (id=1 OR id=2)` $cond = ['and', 'type=1', ['or', 'id=1', 'id=2']] //SQL:`type=1 AND (id=1 OR id=2)` //此写法'='可以换成其他操作符,例:in like != >=等 $cond = [ 'and', ['=', 'type', 1], [ 'or', ['=', 'id', '1'], ['=', 'id', '2'], ] ]
[[or]]:
/SQL:`(type IN (7, 8, 9) OR (id IN (1, 2, 3)))` $cond = ['or', ['type' => [7, 8, 9]], ['id' => [1, 2, 3]]
[[not]]:
//SQL:`NOT (attribute IS NULL)` $cond = ['not', ['attribute' => null]]
[[between]]: not between 用法相同
//SQL:`id BETWEEN 1 AND 10` $cond = ['between', 'id', 1, 10]
[[in]]: not in 用法類似
//SQL:`id IN (1, 2, 3)` $cond = ['in', 'id', [1, 2, 3]] or $cond = ['id'=>[1, 2, 3]]
//IN条件也适用于多字段 $cond = ['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]] //也适用于内嵌sql语句 $cond = ['in', 'user_id', (new Query())->select('id')->from('users')->where(['active' => 1])]
##[[like]]:
//SQL:`name LIKE '%tester%'` $cond = ['like', 'name', 'tester'] //SQL:`name LIKE '%test%' AND name LIKE '%sample%'` $cond = ['like', 'name', ['test', 'sample']] //SQL:`name LIKE '%tester'` $cond = ['like', 'name', '%tester', false]
[[exists]]: not exists用法類似
//SQL:EXISTS (SELECT "id" FROM "users" WHERE "active"=1) $cond = ['exists', (new Query())->select('id')->from('users')->where(['active' => 1])]此外,您可以指定任意運算子如下:
//SQL:`id >= 10` $cond = ['>=', 'id', 10] //SQL:`id != 10` $cond = ['!=', 'id', 10]PHP中文網,有大量免費的
Yii入門教學,歡迎大家學習!
以上是yii框架怎麼條件查詢的詳細內容。更多資訊請關注PHP中文網其他相關文章!

theSostCommonErrorsinyiiframeWorkare“ unknownproperty”,“無效configuration”,“ classNotfound”和“ valianationerationerrors” .1。 “ Unknownerproperty” errorSoccurWhenAccessingNon-existentSistentProperties; SusePropertiesexi; Susepropertiesexi;

歐洲Yii開發者需具備的關鍵技能包括:1.Yii框架精通,2.PHP熟練度,3.數據庫管理,4.前端技能,5.RESTfulAPI開發,6.版本控制系統,7.測試與調試,8.安全知識,9.敏捷方法論,10.軟技能,11.本地化與國際化,12.持續學習,這些技能使開發者在歐洲市場中脫穎而出。

Yes,theYiicommunityisstillactiveandvibrant.1)TheofficialYiiforumremainsaresourcefordiscussionsandsupport.2)TheGitHubrepositoryshowsregularcommitsandpullrequests,indicatingongoingdevelopment.3)StackOverflowcontinuestohostYii-relatedquestionsandhigh-qu

crigatingalaravel projectToyiiishallingButachieffable withiefleflant.1)mapoutlaravel組件likeoutes,控制器和模型。 2)Translatelaravel's sartisancancancommandeloequorentoottooyii的giiandeteverecordeba

軟技能對Yii開發者至關重要,因為它們促進團隊溝通和協作。 1)有效溝通確保項目進展順利,如通過清晰的API文檔和定期會議。 2)協作通過Yii的工具如Gii增強團隊互動,提高開發效率。

laravel'smvCarchitectureOfferSenhancedCodeorganization,改善確定性,andarobustseParefofConcerns.1)ItkeepscodeOdeOrganized,makenavigationNavigationAnvigationAndTeamWorkeer.2)itcompartmentalizestHeaplication,簡化了tompertalizestHeaplication,簡化了tlubloublyingttrublyingtimpertinging和maintenance.3)itse.3)itse

Yiiremainsrelevantinmodernwebdevelopmentforprojectsneedingspeedandflexibility.1)Itoffershighperformance,idealforapplicationswherespeediscritical.2)Itsflexibilityallowsfortailoredapplicationstructures.However,ithasasmallercommunityandsteeperlearningcu

Yii框架在眾多PHP框架中依然保持強大生命力是因為其高效、簡潔和可擴展的設計理念。 1)Yii通過“約定優於配置”提高開發效率;2)基於組件的架構和強大的ORM系統Gii增強了靈活性和開發速度;3)性能優化和不斷的更新迭代確保其持續競爭力。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Dreamweaver Mac版
視覺化網頁開發工具

禪工作室 13.0.1
強大的PHP整合開發環境