FOXDB查詢構建器使用PDO參數綁定來保護您的應用程序免受SQL注入攻擊。無需清潔或消毒字符串作為查詢構建器傳遞給查詢構建器。
composer require webriumfoxdb
配置
選擇
插入語句
更新語句
刪除語句
特殊功能
模式
雄辯
use Foxdb DB ;
use Foxdb Config ;
DB :: addConnection ( ' main ' , [
' host ' => ' localhost ' ,
' port ' => ' 3306 ' ,
' database ' => ' test ' ,
' username ' => ' root ' ,
' password ' => ' 1234 ' ,
' charset ' =>Config:: UTF8 ,
' collation ' =>Config:: UTF8_GENERAL_CI ,
' fetch ' =>Config:: FETCH_CLASS
]);
'main'語句是連接配置的默認名稱
您可以使用DB立面提供的table方法開始查詢。 table方法返回給定表的流利查詢構建器實例,使您可以將更多約束鏈接到查詢中,然後最終使用GET方法檢索查詢的結果:
use Foxdb DB ;
$ users = DB :: table ( ' users ' )-> get ();
foreach ( $ users as $ user ) {
echo $ user -> name ;
}如果您只需要從數據庫表中檢索一行,則可以使用DB立面的first方法。此方法將返回一個stdclass對象:
$ user = DB :: table ( ' users ' )-> where ( ' name ' , ' Jack ' )-> first ();
return $ user -> email ;如果您不需要整個行,則可以使用value方法從記錄中提取單個值。此方法將直接返回列的值:
$ email = DB :: table ( ' users ' )-> where ( ' name ' , ' John ' )-> value ( ' email ' );要通過其id列值檢索單行,請使用find方法:
$ user = DB :: table ( ' users ' )-> find ( 3 ); find方法和first之間的區別在於, first方法(如果存在)以stdclass的形式返回結果,但是find方法以Model的形式返回結果,這為我們提供了更多功能。 (如果值不存在,則兩種方法都返回false。)
?從版本3及以上,查詢可用於查找
$ user = User:: find ( 3 );
if ( $ user ){
$ user -> name = ' Tom ' ;
$ user -> save (); // update name
}
$ user = User:: where ( ' phone ' , ' 09999999999 ' )-> find ();
if ( $ user ){
$ user -> phone = ' 09999999998 ' ;
$ user -> save (); // update user phone number
}您可以使用pluck方法。在此示例中,我們將檢索一個用戶標題的集合:
use Foxdb DB ;
$ titles = DB :: table ( ' users ' )-> pluck ( ' title ' );
foreach ( $ titles as $ title ) {
echo $ title ;
}您可以通過向Pulluck方法提供第二個參數來指定所得集合應將其用作其鍵的列:
$ titles = DB :: table ( ' users ' )-> pluck ( ' title ' , ' name ' );
foreach ( $ titles as $ name => $ title ) {
echo $ title ;
}如果您需要使用數千個數據庫記錄,請考慮使用DB立面提供的chunk方法。該方法一次檢索一小部分結果,並將每個塊進食封閉以進行處理。例如,讓我們一次以100個記錄的塊來檢索整個用戶表:
use Foxdb DB ;
DB :: table ( ' users ' )-> orderBy ( ' id ' )-> chunk ( 100 , function ( $ users ) {
foreach ( $ users as $ user ) {
//
}
});您可能會通過從關閉中返回false來阻止其他塊被處理:
DB :: table ( ' users ' )-> orderBy ( ' id ' )-> chunk ( 100 , function ( $ users ) {
// Process the records...
return false ;
});您可以使用each方法。
use Foxdb DB ;
DB :: table ( ' users ' )-> orderBy ( ' id ' )-> each ( function ( $ user ) {
//
});FOXDB創建了一個簡單的分頁方法。在下面的示例中,結果數限制為10個記錄,您可以通過更改頁碼來獲取信息。
$ page = 1 ;
$ list = DB :: table ( ' posts ' )
-> is ( ' active ' )
-> paginate ( 10 , $ page );它的輸出是包含以下屬性的stdClass:
$ list -> total ; // The total number of rows
$ list -> count ; // The number of rows received on the current page
$ list -> per_page ; // The number of rows to display on each page
$ list -> prev_page ; // Previous page number. If not available, its value is false
$ list -> next_page ; // next page number. If not available, its value is false
$ list -> current_page ; // Current page number
$ list -> data ; // List of data rows查詢構建器還提供了各種方法來檢索諸如count , max , min , avg和sum等骨料值。構建查詢後,您可以調用這些方法中的任何一種:
use Foxdb DB ;
$ users = DB :: table ( ' users ' )-> count ();
$ price = DB :: table ( ' orders ' )-> max ( ' price ' );當然,您可以將這些方法與其他條款相結合,以微調如何計算總價值:
$ price = DB :: table ( ' orders ' )
-> where ( ' finalized ' , 1 )
-> avg ( ' price ' );您可以使用COUNT方法來確定是否存在與查詢約束相匹配的任何記錄,而是可以使用“存在”和“無關”方法:
if ( DB :: table ( ' orders ' )-> where ( ' finalized ' , 1 )-> exists ()) {
// ...
}
if ( DB :: table ( ' orders ' )-> where ( ' finalized ' , 1 )-> doesntExist ()) {
// ...
}指定選擇子句
您可能並不總是想從數據庫表中選擇所有列。使用select方法,您可以為查詢指定自定義“選擇”子句:
use Foxdb DB ;
$ users = DB :: table ( ' users ' )
-> select ( ' name ' , ' email as user_email ' )
-> get ();
// Or you can send as an array
$ users = DB :: table ( ' users ' )
-> select ([ ' name ' , ' email as user_email ' ])
-> get ();但是有一種更現代的方式可以做到這一點。您可以像下面的示例一樣行事
$ users = DB :: table ( ' users ' )
-> select ( function ( $ query ){
$ query -> field ( ' name ' );
$ query -> field ( ' email ' )-> as ( ' user_email ' );
})
-> get ();有時,您可能需要將任意字符串插入查詢中。要創建一個原始的字符串表達式,您可以使用DB立面提供的raw方法:
$ users = DB :: table ( ' users ' )
-> select ( DB :: raw ( ' count(*) as user_count, status ' ))
-> where ( ' status ' , ' <> ' , 1 )
-> groupBy ( ' status ' )
-> get ();在RAW中使用該參數如下示例
DB::raw('count(?)',['id'])
配x 原始語句將作為字符串注入查詢,因此您應該非常小心,以免產生SQL注入漏洞。
但是為此,最好使用以下方法避免SQL injection攻擊
$ users = DB :: table ( ' users ' )
-> select ( function ( $ query ){
$ query -> count ( ' * ' )-> as ( ' user_count ' )
$ query -> field ( ' status ' );
})
-> get ();在此結構中,您可以訪問field , count , sum , avg , min , max all as方法。
您還可以使用以下方法將原始表達式插入查詢的各個部分。請記住,FOXDB不能保證使用RAW表達式的任何查詢都受到SQL注入漏洞的保護。
Whereraw和Orwhereraw方法可用於將原始“”子句注入查詢中。這些方法接受可選的綁定作為其第二個論點:
$ orders = DB :: table ( ' orders ' )
-> whereRaw ( ' price > IF(state = "TX", ?, 100) ' , [ 200 ])
-> get ();HaveRaw和OrhavingRaw方法可用於提供一個原始字符串作為“ home”子句的值。這些方法接受可選的綁定作為其第二個論點:
$ orders = DB :: table ( ' orders ' )
-> select ( ' department ' , DB :: raw ( ' SUM(price) as total_sales ' ))
-> groupBy ( ' department ' )
-> havingRaw ( ' SUM(price) > ? ' , [ 2500 ])
-> get ();查詢構建器也可用於將“加入”子句添加到您的查詢中。要執行基本的“內聯合”,您可以在查詢構建器實例上使用JOIN方法。傳遞給加入方法的第一個參數是您需要加入的表的名稱,而其餘參數則指定加入的列約束。您甚至可以在單個查詢中加入多個表:
use Foxdb DB ;
$ users = DB :: table ( ' users ' )
-> join ( ' contacts ' , ' users.id ' , ' = ' , ' contacts.user_id ' )
-> join ( ' orders ' , ' users.id ' , ' = ' , ' orders.user_id ' )
-> select ( ' users.* ' , ' contacts.phone ' , ' orders.price ' )
-> get ();在FOXDB中,您可以更輕鬆地做到這一點
$ users = DB :: table ( ' users ' )
-> select ( ' users.* ' , ' orders.price ' )
-> join ( ' orders.user_id ' , ' users.id ' )
-> get ();在此結構中,您要輸入要使用其外鍵( 'orders.user_id' )的表格名稱,然後輸入主密鑰( 'user.id' )。
如果您想執行“左聯接”或“右JOIN”而不是“內聯接”,請使用左鍵或右Join方法。這些方法具有與聯接方法相同的簽名:
$ users = DB :: table ( ' users ' )
-> leftJoin ( ' posts ' , ' users.id ' , ' = ' , ' posts.user_id ' )
-> get (); $ users = DB :: table ( ' users ' )
-> rightJoin ( ' posts ' , ' users.id ' , ' = ' , ' posts.user_id ' )
-> get ();您可以使用CrossJoin方法執行“交叉聯接”。 Cross Join在第一張桌子和連接表之間生成笛卡爾產品:
$ sizes = DB :: table ( ' sizes ' )
-> crossJoin ( ' colors ' )
-> get ();您可以使用查詢構建器的方法在其中添加“位置”子句中查詢的方法。對Where方法的最基本呼籲需要三個參數。第一個參數是列的名稱。第二個參數是操作員,它可以是數據庫支持的運算符。第三個參數是與列的值進行比較的值。
例如,以下查詢檢索用戶,其中投票列的值等於100,而年齡列的值大於35:
$ users = DB :: table ( ' users ' )
-> where ( ' votes ' , ' = ' , 100 )
-> where ( ' age ' , ' > ' , 35 )
-> get ();為了方便起見,如果要驗證列為給定值=給定值,則可以將值作為第二個參數傳遞給Where方法。 FOXDB將假設您想使用=操作員:
$ users = DB :: table ( ' users ' )-> where ( ' votes ' , 100 )-> get ();如前所述,您可以使用數據庫系統支持的任何操作員:
$ users = DB :: table ( ' users ' )
-> where ( ' votes ' , ' >= ' , 100 )
-> get (); $ users = DB :: table ( ' users ' )
-> where ( ' votes ' , ' <> ' , 100 )
-> get (); $ users = DB :: table ( ' users ' )
-> where ( ' name ' , ' like ' , ' T% ' )
-> get ();將鏈接在一起呼叫查詢構建器的方法時,將使用和運算符將條款連接在一起。但是,您可以使用OR Where方法使用或運算符將子句加入查詢。 Or Where方法接受與Where方法相同的參數:
$ users = DB :: table ( ' users ' )
-> where ( ' votes ' , ' > ' , 100 )
-> orWhere ( ' name ' , ' John ' )
-> get ();如果您需要在括號內分組“或”條件,則可以將關閉作為第一個參數將其傳遞給ORWHORE方法:
$ users = DB :: table ( ' users ' )
-> where ( ' votes ' , ' > ' , 100 )
-> orWhere ( function ( $ query ) {
$ query -> where ( ' name ' , ' Abigail ' )
-> where ( ' votes ' , ' > ' , 50 );
})
-> get ();上面的示例將產生以下SQL:
select * from users where votes > 100 or (name = 'Abigail' and votes > 50)
whereNot和orWhereNot方法可用於否定給定的一組查詢約束。例如,以下查詢不包括在清關或價格少於10的產品:
$ products = DB :: table ( ' products ' )
-> whereNot ( function ( $ query ) {
$ query -> where ( ' clearance ' , true )
-> orWhere ( ' price ' , ' < ' , 10 );
})
-> get ();方法whereBetween驗證了列的值是否在兩個值之間:
$ users = DB :: table ( ' users ' )
-> whereBetween ( ' votes ' , [ 1 , 100 ])
-> get ();方法之間的whereNotBetween驗證了列的值是否在兩個值之外:
$ users = DB :: table ( ' users ' )
-> whereNotBetween ( ' votes ' , [ 1 , 100 ])
-> get (); whereIn方法驗證給定列的值是否包含在給定數組中:
$ users = DB :: table ( ' users ' )
-> whereIn ( ' id ' , [ 1 , 2 , 3 ])
-> get (); whereNotIn方法驗證給定列的值是否在給定數組中不包含:
$ users = DB :: table ( ' users ' )
-> whereNotIn ( ' id ' , [ 1 , 2 , 3 ])
-> get (); whereNull方法驗證給定列的值是null:
$ users = DB :: table ( ' users ' )
-> whereNull ( ' updated_at ' )
-> get (); whereNotNull方法驗證了列的值不是null:
$ users = DB :: table ( ' users ' )
-> whereNotNull ( ' updated_at ' )
-> get (); whereDate方法可用於將列的值與日期進行比較:
$ users = DB :: table ( ' users ' )
-> whereDate ( ' created_at ' , ' 2016-12-31 ' )
-> get (); whereMonth方法可用於將列的值與特定月份進行比較:
$ users = DB :: table ( ' users ' )
-> whereMonth ( ' created_at ' , ' 12 ' )
-> get (); whereDay方法可用於將列的價值與本月的特定日期進行比較:
$ users = DB :: table ( ' users ' )
-> whereDay ( ' created_at ' , ' 31 ' )
-> get (); whereYear可以使用其中的方法將列的價值與特定年份進行比較:
$ users = DB :: table ( ' users ' )
-> whereYear ( ' created_at ' , ' 2016 ' )
-> get ();可以使用whereTime方法將列的值與特定時間進行比較:
$ users = DB :: table ( ' users ' )
-> whereTime ( ' created_at ' , ' = ' , ' 11:20:45 ' )
-> get (); whereColumn方法可用於驗證兩個列相等:
$ users = DB :: table ( ' users ' )
-> whereColumn ( ' first_name ' , ' last_name ' )
-> get ();您也可以將比較操作員傳遞給whereColumn方法:
$ users = DB :: table ( ' users ' )
-> whereColumn ( ' updated_at ' , ' > ' , ' created_at ' )
-> get ();orderBy方法允許您通過給定的列對查詢的結果進行排序。 orderBy方法接受的第一個參數應為您要排序的列,而第二個參數確定了排序的方向,並且可以是ASC或DESC:
$ users = DB :: table ( ' users ' )
-> orderBy ( ' name ' , ' desc ' )
-> get ();要按多列進行排序,您可以根據需要多次調用OrderBy:
$ users = DB :: table ( ' users ' )
-> orderBy ( ' name ' , ' desc ' )
-> orderBy ( ' email ' , ' asc ' )
-> get ();latest和oldest方法使您可以輕鬆按日期訂購結果。默認情況下,結果將由表的created_at列訂購。或者,您可以傳遞要排序的列名:
$ user = DB :: table ( ' users ' )
-> latest ()
-> first ();inrandomorder方法可用於隨機對查詢結果進行分類。例如,您可以使用此方法獲取隨機用戶:
$ randomUser = DB :: table ( ' users ' )
-> inRandomOrder ()
-> first ();groupBy & having方法正如您可能期望的那樣,可以使用groupBy和having方法來對查詢結果進行分組。 having方法的簽名類似於其中的方法:
$ users = DB :: table ( ' users ' )
-> groupBy ( ' account_id ' )
-> having ( ' account_id ' , ' > ' , 100 )
-> get ();您可以將多個參數傳遞給Groupby方法,以通過多列分組:
$ users = DB :: table ( ' users ' )
-> groupBy ( ' first_name ' , ' status ' )
-> having ( ' account_id ' , ' > ' , 100 )
-> get ();要構建更高級的語句,請參見HaveRaw方法。
您可以使用skip並take方法來限制從查詢返回的結果數或跳過查詢中的給定數量的結果:
$ users = DB :: table ( ' users ' )-> skip ( 10 )-> take ( 5 )-> get ();另外,您可以使用limit和offset方法。這些方法在功能上分別等同於採用和跳過方法:
$ users = DB :: table ( ' users ' )
-> offset ( 10 )
-> limit ( 5 )
-> get ();查詢構建器還提供了一種insert方法,可用於將記錄插入數據庫表中。插入方法接受列名稱和值的數組:
DB :: table ( ' users ' )-> insert ([
' email ' => ' [email protected] ' ,
' votes ' => 0
]);如果該表具有自動插入ID,請使用insertGetId方法插入記錄,然後檢索ID:
$ id = DB :: table ( ' users ' )-> insertGetId (
[ ' email ' => ' [email protected] ' , ' votes ' => 0 ]
);除了將記錄插入數據庫外,查詢構建器還可以使用更新方法更新現有記錄。像插入方法一樣,更新方法接受列和值對的數組,指示要更新的列。更新方法返回受影響的行的數量。您可以使用條款來約束更新查詢:
$ affected = DB :: table ( ' users ' )
-> where ( ' id ' , 1 )
-> update ([ ' votes ' => 1 ]);查詢構建器還提供了方便的方法來增加或減少給定列的值。這兩種方法都至少接受一個參數:要修改的列。可以提供第二個參數來指定應遞增或減少列的數量:
DB :: table ( ' users ' )-> increment ( ' votes ' );
DB :: table ( ' users ' )-> increment ( ' votes ' , 5 );
DB :: table ( ' users ' )-> decrement ( ' votes ' );
DB :: table ( ' users ' )-> decrement ( ' votes ' , 5 ); DB :: table ( ' users ' )-> where ( ' id ' , $ id )-> delete ();您可以使用更有趣的語法,除了縮短代碼外,還可以幫助使代碼更可讀
要基於布爾值創建查詢,您可以使用true和false is方法
$ active_list = DB :: table ( ' users ' )-> is ( ' active ' )-> get ();
// OR
$ active_list = DB :: table ( ' users ' )-> true ( ' active ' )-> get (); $ inactive_list = DB :: table ( ' users ' )-> is ( ' active ' , false )-> get ();
//OR
$ inactive_list = DB :: table ( ' users ' )-> false ( ' active ' )-> get ();您無需連續查詢使用的方法。您可以使用和方法或使用,也可以使用而不是在哪裡。
例子:
DB :: table ( ' users ' )
-> is ( ' active ' )
-> and ( ' credit ' , ' > ' , 0 )
-> or ( ' vip ' , true )
-> get (); DB :: table ( ' users ' )
-> in ( ' id ' , [ 1 , 5 , 10 ])
-> get ();其他方法也可用,例如以下方法:
not(..) / orNot(..)
in(..) / notIn(..) / orIn(..) / orNotIn(..)中like(..) / orLike(..)
null(..) / orNull(..) / notNull(..) / orNotNull(..)
date(..) / orDate(..)
year(..) / orYear(..)
month(..) / orMonth(..)
day(..) / orDay(..)
time(..) / orTime(..)