Data.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 97
  5. */
  6. namespace app\controller;
  7. use app\BaseController;
  8. use think\facade\Db;//第一种 通过门面模式的方式获取
  9. use app\model\Demo;
  10. class Data extends BaseController{
  11. /**
  12. * 通过模型的方法获取 数据库数据
  13. * @throws \think\db\exception\DataNotFoundException
  14. * @throws \think\db\exception\DbException
  15. * @throws \think\db\exception\ModelNotFoundException
  16. */
  17. public function model1(){
  18. $result = Demo::find(2);
  19. dump($result->toArray());
  20. }
  21. public function model2(){
  22. $modelObj = new Demo();
  23. $result = $modelObj->where("category_id",3)
  24. ->limit(2)
  25. ->select();
  26. // dump($result);
  27. foreach ($result as $resultvar){
  28. // dump($resultvar->title);
  29. dump($resultvar['title']);
  30. dump($resultvar->status_text);
  31. }
  32. }
  33. /**
  34. * 查询数据
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\DbException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. */
  39. public function index(){
  40. // //第一种 通过门面模式的方式获取
  41. // $result = Db::table("mall_demo")->where("id",2)->find();
  42. // dump($result);
  43. // //通过容器的方式来获取
  44. // $result = app("db")->table("mall_demo")->where("id",2)->find();
  45. // dump($result);
  46. //获取一条
  47. $result = Db::table("mall_demo")
  48. ->order("id","desc")
  49. ->find();
  50. dump($result);
  51. //获取获取多条
  52. $result = Db::table("mall_demo")
  53. ->order("id","desc")
  54. ->limit(2,2) //可以做分页的逻辑 --- 这个需要计算开始值
  55. ->select();
  56. dump($result);
  57. //获取获取多条 -- 分页方式
  58. $result = Db::table("mall_demo")
  59. ->order("id","desc")
  60. ->page(1,2) //可以做分页的逻辑 --- 这个不需要计算开始值
  61. ->select();
  62. dump($result);
  63. //获取获取多条 -- 范围值
  64. $result = Db::table("mall_demo")
  65. ->where("id",">",2)
  66. ->select();
  67. dump($result);
  68. //获取获取多条 -- 多条件
  69. $result = Db::table("mall_demo")
  70. ->where("id",">",2)
  71. ->where("category_id","=",3)
  72. ->select();
  73. dump($result);
  74. //获取获取多条 -- 多条件
  75. $result = Db::table("mall_demo")
  76. ->where([
  77. ["id",">",2],
  78. ["category_id","=",3],
  79. ["id","in","4,5,6"]
  80. ])
  81. ->select();
  82. dump($result);
  83. }
  84. /**
  85. * 打印sql
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\DbException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. */
  90. public function abc(){
  91. //第一种 --- 打印sql
  92. $result = Db::table("mall_demo")->where("id",2)->fetchSql()->find();
  93. dump($result);
  94. //第二种 --- 打印sql
  95. $result = Db::table("mall_demo")->where("id",2)->find();
  96. echo Db::getLastSql();
  97. exit;
  98. }
  99. /**
  100. * 新增逻辑
  101. */
  102. public function demo(){
  103. $data = [
  104. "title" => "深刻的反思就",
  105. "category_id" => 4,
  106. ];
  107. $result = Db::table("mall_demo")->insert($data);
  108. echo Db::getLastSql();
  109. dump($result);
  110. }
  111. /**
  112. * 真正删除逻辑
  113. */
  114. public function demodel(){
  115. $result = Db::table("mall_demo")->where("id",1)->delete(1);
  116. echo Db::getLastSql();
  117. dump($result);
  118. }
  119. /**
  120. * 真正更新逻辑
  121. */
  122. public function demoupdate(){
  123. $result = Db::table("mall_demo")
  124. ->where("id",2)
  125. ->update([
  126. "title" => "真是的哎",
  127. ]);
  128. echo Db::getLastSql();
  129. dump($result);
  130. }
  131. }