123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- /**
- * Created by PhpStorm.
- * User: 97
- */
- namespace app\controller;
- use app\BaseController;
- use think\facade\Db;//第一种 通过门面模式的方式获取
- use app\model\Demo;
- class Data extends BaseController{
- /**
- * 通过模型的方法获取 数据库数据
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function model1(){
- $result = Demo::find(2);
- dump($result->toArray());
- }
- public function model2(){
- $modelObj = new Demo();
- $result = $modelObj->where("category_id",3)
- ->limit(2)
- ->select();
- // dump($result);
- foreach ($result as $resultvar){
- // dump($resultvar->title);
- dump($resultvar['title']);
- dump($resultvar->status_text);
- }
- }
- /**
- * 查询数据
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function index(){
- // //第一种 通过门面模式的方式获取
- // $result = Db::table("mall_demo")->where("id",2)->find();
- // dump($result);
- // //通过容器的方式来获取
- // $result = app("db")->table("mall_demo")->where("id",2)->find();
- // dump($result);
- //获取一条
- $result = Db::table("mall_demo")
- ->order("id","desc")
- ->find();
- dump($result);
- //获取获取多条
- $result = Db::table("mall_demo")
- ->order("id","desc")
- ->limit(2,2) //可以做分页的逻辑 --- 这个需要计算开始值
- ->select();
- dump($result);
- //获取获取多条 -- 分页方式
- $result = Db::table("mall_demo")
- ->order("id","desc")
- ->page(1,2) //可以做分页的逻辑 --- 这个不需要计算开始值
- ->select();
- dump($result);
- //获取获取多条 -- 范围值
- $result = Db::table("mall_demo")
- ->where("id",">",2)
- ->select();
- dump($result);
- //获取获取多条 -- 多条件
- $result = Db::table("mall_demo")
- ->where("id",">",2)
- ->where("category_id","=",3)
- ->select();
- dump($result);
- //获取获取多条 -- 多条件
- $result = Db::table("mall_demo")
- ->where([
- ["id",">",2],
- ["category_id","=",3],
- ["id","in","4,5,6"]
- ])
- ->select();
- dump($result);
- }
- /**
- * 打印sql
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function abc(){
- //第一种 --- 打印sql
- $result = Db::table("mall_demo")->where("id",2)->fetchSql()->find();
- dump($result);
- //第二种 --- 打印sql
- $result = Db::table("mall_demo")->where("id",2)->find();
- echo Db::getLastSql();
- exit;
- }
- /**
- * 新增逻辑
- */
- public function demo(){
- $data = [
- "title" => "深刻的反思就",
- "category_id" => 4,
- ];
- $result = Db::table("mall_demo")->insert($data);
- echo Db::getLastSql();
- dump($result);
- }
- /**
- * 真正删除逻辑
- */
- public function demodel(){
- $result = Db::table("mall_demo")->where("id",1)->delete(1);
- echo Db::getLastSql();
- dump($result);
- }
- /**
- * 真正更新逻辑
- */
- public function demoupdate(){
- $result = Db::table("mall_demo")
- ->where("id",2)
- ->update([
- "title" => "真是的哎",
- ]);
- echo Db::getLastSql();
- dump($result);
- }
- }
|