Blogger Information
Blog 94
fans 0
comment 0
visits 92097
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
TP5模型操作
可乐随笔
Original
1217 people have browsed it

模型概念理解:

注:用户访问URL对应的是一个控制器,控制器与模型(数据库)交互,然后把处理完的数据前给视图,视图负责展现数据。

1.是跟一张数据表绑定的,模型名和数据库名称一致。
2.在模块中增加model目录,model下面的模型文件名称必须和数据库中的表名完全一致,做到一一对应。
3.引入Student模型后,这张表就模型化了,可以直接模型名直接操作数据表的内容。

  1. <?php
  2. namespace app\index\model;
  3. use think\Model;
  4. /*
  5. * 模型名和数据库名称一致,首字母大写
  6. */
  7. class Student extends Model
  8. {
  9. }

控制器中方法操作模型的示例:

  1. <?php
  2. namespace app\index\controller;
  3. use app\index\model\Student;
  4. /*
  5. * 模型是跟一张数据表绑定的,模型名和数据库名称一致,首字母大写
  6. * 引入Student模型后,这张表就模型化了,可以直接模型名直接操作数据表的内容
  7. * student:: 等价于 Db::table('student')
  8. */
  9. class Demo6
  10. {
  11. public function get()
  12. {
  13. // dump(Student::get(3));
  14. //用查询构造器创建更加复杂的查询
  15. $res = Student::field('id,name,email')
  16. ->where('id',3)
  17. ->find();
  18. return $res;
  19. }
  20. public function all()
  21. {
  22. // dump(Student::all());
  23. // dump(Student::all([1,2,3]));
  24. //用查询构造器创建更加复杂的查询
  25. $res = Student::field(['name'=>'姓名','email'=>'邮箱'])
  26. ->where('id','in','4,2,9')
  27. ->select();
  28. //Student::返回的是模型对象,Db::table()返回的是数组
  29. return $res;
  30. }
  31. }
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!