站三界导航
首页 TP代码tp5三种查询数据的方式

tp5三种查询数据的方式

  • TP代码
  • 来源:站三界导航
  • 143阅读
  • 2022-05-06

方式一:原生sql查询

代码示例:


<?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;
    }
}
方式二:使用查询构建器
代码示例:
<?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:
<?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:



<?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;
    }
}
本文结束
本文来自投稿,不代表站三界导航立场,如若转载,请注明出处:https://www.zhansanjie.com/article/details/8330.html

版权声明:

1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。

2、本站仅提供信息发布平台,不承担相关法律责任。

3、若侵犯您的版权或隐私,请联系本站管理员删除。

4、本文由会员转载自互联网,如果您是文章原创作者,请联系本站注明您的版权信息。

分享
站三界导航
本站声明:本站严格遵守国家相关法律规定,非正规网站一概不予收录。本站所有资料取之于互联网,任何公司或个人参考使用本资料请自辨真伪、后果自负,站三界导航不承担任何责任。在此特别感谢您对站三界导航的支持与厚爱。