think2019-6.5
![](https://img2018.cnblogs.com/blog/1513116/201906/1513116-20190606023036878-1974707705.png)
【1】模型对象:Model
理解:模型是一个类,对应是一个表,new 模型==表对象
关联模型:关联表
![](https://img2018.cnblogs.com/blog/1513116/201906/1513116-20190606023132817-1319361007.png)
![](https://img2018.cnblogs.com/blog/1513116/201906/1513116-20190606023203268-1013476717.png)
取得表(模型/对象)中的数据
(new ClassesModel())->all()
(new ClassesModel())->get($id)
(new ClassesModel())->save()
(new ClassesModel())->delete()
准备保存到表(模型/对象)
$user=new UsersModel
$user->email = input('email');
$user->password = input('password');
【2】Request对象
public function urlpost(Request $r){ //
p($r->post());
p($r->post('name1'));
【3】验证器对象Validate
$all = Request::instance()->param();
$data =['chs'=>$chs];
$obj = new IsValidate3();
$rs = $obj->batch()->check($data);
if($rs){
echo $chs.'是汉字';
} else{
echo $chs.'不是汉字';
}
【4】控制器对象Controller
$this->redirect()
$this->assign()
$this->fetch
【5】路由对象
应用/v1/控制器/方法
Route::rule('index','index/v1.Classes/index');
Route::rule('index1','index/v1.Classes/index1');
【6】Db对象
Db::query('原生sql')返回二维数组
Db::execute('原生sql') 返回值是影响的记录行数
如果没有设置表前缀
Db::table('Think_tableName')->insert(array(''=>''))
Db::table()->where()->select()
Db::table()->where()->update
Db::table()->where()->delete()
如果设置表前缀
Db::name('tableName')
->where('id','=','1') =,>,<,>=,=<,like,in,between
->where('name','like','%wangpan%')
->where('id','in',[1,2,3])
->where('id','between',[5,8])
->where(array('id'=>array('between','1,3'),'name'=>array('like','%think%')))
->where('id&status','>',0) //&&
->where('id|status','>',0) //||
->where()->where()->where()->limit(10)->select()
<?php namespace app\index\controller\v1; use \app\index\controller\model\Classes as ClassesModel; use think\Controller; use think\Request; use think\Url; class Classes extends Controller { public function index(){ //获取表所有 $classes = (new ClassesModel())->all(); $this->assign('classes',$classes); return $this->fetch(); } public function index1(){//分页显示数据 $classes = (new ClassesModel())->paginate(3);//每页显示3条 $this->assign('classes',$classes); return $this->fetch(); } public function url(){//生成url // echo Url::build('Classes/index1'); // echo url('Classes/index1');//助手函数 return $this->fetch(); } public function urlpost(Request $r){ //【1】参数中使用请求对象 p($r->post()); p($r->post('name1')); $classesModel = new \app\index\controller\model\Classes(); if( $classesModel->save($r->post())){ echo '添加成功'; // $this->success('新增成功', 'index'); $this->redirect('index',302); }else{ $this->error('新增失败'); } /* 【2】直接使用 // $post=Request::instance()->param(); // p(Request::instance()->param('name1')); // p($post); // p($r); */ /* 【3】助收函数 $post= input('param.'); p(input('param.name1')); p($post) ; */ } public function ajax(){ $arr=array('a'=>'aaa','b'=>'bbb'); return json_encode($arr); } //查 public function getAllClasses(){ $obj= ($this->thisModel())->all(); //取得所有表的模型对象 $classAll=[]; foreach ($obj as $k=>$v){ $classAll[]=$v->toArray(); } p ($classAll); $this->assign('classesAll',$classAll); return $this->fetch(); } //增 public function addClass(){ $classModel=new ClassesModel(); $classModel->classes_name='7班级'; if ($classModel->save()) { return '用户[ ' . $classModel->classes_name . ':' . $classModel->id . ' ]新增成功'; } else { return $classModel->getError(); } } public function addClass2($v){ echo (new ClassesModel())->addClass2($v); } //改 public function updateClass($id){ $classOneObjModel=ClassesModel::get($id); $classOneObjModel->classes_name="修改的x班"; $classOneObjModel->save(); return '更新数据成功'; } //删 public function deleteClass($id){ $classOneObjModel=ClassesModel::get($id); if($classOneObjModel){ $classOneObjModel->delete(); echo '删除成功'; // $this->redirect('index',302); }else{ echo '删除失败'; } } public function thisModel(){ return new \app\index\controller\model\Classes(); } }
注意模板要return:return $this->fetch();
<td><a href='{:url("index/v1.Classes/deleteClass",['id'=>$banji.id])}'>删除</a></td>
用户注册ajax,validate: think https://blog.csdn.net/angryshan/article/details/81240138