注:用户访问URL对应的是一个控制器,控制器与模型(数据库)交互,然后把处理完的数据前给视图,视图负责展现数据。
1.是跟一张数据表绑定的,模型名和数据库名称一致。
2.在模块中增加model目录,model下面的模型文件名称必须和数据库中的表名完全一致,做到一一对应。
3.引入Student模型后,这张表就模型化了,可以直接模型名直接操作数据表的内容。
<?phpnamespace app\index\model;use think\Model;/** 模型名和数据库名称一致,首字母大写*/class Student extends Model{}
控制器中方法操作模型的示例:
<?phpnamespace app\index\controller;use app\index\model\Student;/** 模型是跟一张数据表绑定的,模型名和数据库名称一致,首字母大写* 引入Student模型后,这张表就模型化了,可以直接模型名直接操作数据表的内容* student:: 等价于 Db::table('student')*/class Demo6{public function get(){// dump(Student::get(3));//用查询构造器创建更加复杂的查询$res = Student::field('id,name,email')->where('id',3)->find();return $res;}public function all(){// dump(Student::all());// dump(Student::all([1,2,3]));//用查询构造器创建更加复杂的查询$res = Student::field(['name'=>'姓名','email'=>'邮箱'])->where('id','in','4,2,9')->select();//Student::返回的是模型对象,Db::table()返回的是数组return $res;}}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号