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(..)