本文实例讲述了ThinkPHP5查询数据及处理结果的方法。分享给大家供大家参考,具体如下:
在处理数据库查询结果时遇到了些问题,记录下用到过的几种查询方式和结果处理。
1. 查询某条记录
1
2
3
4
5
6
7
8
9
10
11
12
|
$where = array ( "version_id" => $version_id ); $data = model( "PackageWhitelist" )->where( $where )->find(); $this ->assign( "package_id" , $package_id ); $where = array ( "package_id" => $package_id ); $data = model( "Package" )->where( $where )->find(); if ( $data ){ $this ->assign( "target_version" , $data [ 'target_version' ]); } |
2. 查询某条记录某个字段
1
|
$device_number_list = model( "PackageWhitelist" )->where( $where )->field( "device_number" )->find(); |
1
|
$this ->assign( "device_number" , $device_number_list [ 'device_number' ]); |
3. 查询多个记录某个字段 , 并处理结果,结果是一个数组集
1
2
3
4
5
6
7
8
9
10
11
12
13
|
$where = array ( "version_id" => $version_id ); $data = model( "PackageWhitelist" )->where( $where )->field( "device_number" )->select(); $device_number_list = '' ; foreach ( $data as $val ){ $list = $val ->toArray(); if ( $device_number_list ){ $device_number_list = $device_number_list . ';' . $list [ "device_number" ]; } else { $device_number_list = $list [ "device_number" ]; } } |
4. 查询多条记录
1
2
3
4
5
6
7
8
9
10
11
12
13
|
$where = array ( "version_id" => $version_id ); $data = model( "PackageWhitelist" )->where( $where )->select(); $device_number_list = '' ; foreach ( $data as $val ){ $list = $val ->toArray(); if ( $device_number_list ){ $device_number_list = $device_number_list . ';' . $list [ "device_number" ]; } else { $device_number_list = $list [ "device_number" ]; } } |
5. 以页形式查询,并处理结果。
1
2
3
4
5
6
7
8
9
10
11
12
|
public function index( $version_id ){ $where = array ( "version_id" => $version_id ); $version_name = model( "Version" )->where( $where )->field( "version_name" )->find(); $listrows =config( "LISTROWS" )?config( "LISTROWS" ):10; $package_lists =model( "Package" )->where( $where )->paginate( $listrows ); $package_infos = $package_lists ->toArray()[ "data" ]; foreach ( $package_infos as $key => $value ){ $package_infos [ $key ] = array ( "source_version" => $version_name [ "version_name" ]) + $package_infos [ $key ]; } } |
再来总结一下TP5的三种查询数据库方式
方式一:原生sql查询
代码示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?php /** * Created by PhpStorm. * User: chenzhitao * Date: 2017/5/8 * Time: 下午2:15 */ namespace app\api\model; use think\Db; use think\Exception; class Banner { public static function getBannerByID( $id ){ $result = Db::query( 'select * from banner_item where banner_id=?' ,[ $id ]); return $result ; } } |
方式二:使用查询构建器
代码示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<?php /** * Created by PhpStorm. * User: chenzhitao * Date: 2017/5/8 * Time: 下午2:15 */ namespace app\api\model; use think\Db; use think\Exception; class Banner { public static function getBannerByID( $id ){ //1.使用原生sql // $result = Db::query('select * from banner_item where banner_id=?',[$id]); // return $result; //2.使用查询构建器 /* * 链式查询Db::table('banner_item')->where('banner_id','=',$id) 返回查询对象,->select();返回查询结果, * 除了select操作还有 find(返回一条数据) update delete insert * 对应的where 也分三种,1.表达式where('字段名','表达式','查询条件') 2.数组发 3.闭包。 */ // 2.1 表达式法 // $result = Db::table('banner_item') // ->where('banner_id','=',$id) // ->select(); // return $result; //2.2 闭包法 $result = Db::table( 'banner_item' ) ->where( function ( $query ) use ( $id ){ $query ->where( 'banner_id' , '=' , $id ); }) ->select(); return $result ; } } |
方式三:ORM(Object Relation Mapping) 对象关系映射
使用ORM 查询数据库主要区别就是在写模型的继承think\model类,然后控制器就可以使用model的默认方法来获取数据而不是自己再在模型中专门写一个获取方法
代码示例:
model:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
<?php /** * Created by PhpStorm. * User: chenzhitao * Date: 2017/5/8 * Time: 下午2:15 */ namespace app\api\model; use think\Db; use think\Model; class Banner extends Model { // public static function getBannerByID($id){ // //1.使用原生sql //// $result = Db::query('select * from banner_item where banner_id=?',[$id]); //// return $result; // //2.使用查询构建器 // /* // * 链式查询Db::table('banner_item')->where('banner_id','=',$id) 返回查询对象,->select();返回查询结果, // * 除了select操作还有 find(返回一条数据) update delete insert // * 对应的where 也分三种,1.表达式where('字段名','表达式','查询条件') 2.数组发 3.闭包。 // */ // // // 2.1 表达式法 //// $result = Db::table('banner_item') //// ->where('banner_id','=',$id) //// ->select(); //// return $result; // //2.2 闭包法 // $result = Db::table('banner_item') // ->where(function ($query) use($id){ // $query->where('banner_id','=',$id); // // }) // ->select(); // return $result; // // // // // // } } |
controller:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?php /** * Created by PhpStorm. * User: chenzhitao * Date: 2017/5/7 * Time: 下午1:49 */ namespace app\api\controller\v1; use app\api\validate\IDMustBePositiveInt; use app\lib\exception\BannerMissException; use app\api\model\Banner as BannerModel; class Banner { public function getBanner( $id ){ //调用验证器 ( new IDMustBePositiveInt())->goCheck(); // $banner = BannerModel::getBannerByID($id); $banner = BannerModel::get( $id ); if (! $banner ){ throw new BannerMissException(); } return $banner ; } } |
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。
原文链接:http://blog.csdn.net/tww85/article/details/52880405