thinkphp简介

控制器

自动搜索控制器 'controller_auto_search' => true, 只有在应用配置文件里修改才起做用api

多级控制器 路由:'api/v1/:a/:b/:c' => 'api/v1.:a.:b/:c'数组

路由

'api/v1/user/saler/auth'=>'index/index/index',app

'api/v1/user/saler/auth'=>'api/v1.user.Saler/auth',this

模型

  • 查询

模型查询出来的结果都是模型对象或模型对象的集合url

$user = new UserModel();
$output = $user->field('userid,name')->where('userid', $userid)->find();
$list = $user->field('userid,name')->where('userid', $userid)->whereor('role',2)->limit(0,10)->select();
  • 子查询
$list = Db::table('t_product')
		->where('productid','IN',function($query) use($myid) {
			$query->table('t_follow_product')->where('userid',$myid)->field('productid');
		})
		->column('productid,name');
  • 关联查找
$m = new AuthModel();
$m->field('userid,auth_state') // 必须得有userid,不然hasOne不会触发
    ->with('user')
    ->where('userid',$userid)
    ->find();
class Auth extends Model {
    public function user() {
	return $this->hasOne('User','userid')->bind('role,name,head_img_url');
    }
}
  • 删除

删除条件必须是主键,若是不是主键,必须用wherecode

UserModel::destroy(['userid'=>$userid]);
$m->where('name',$name)->delete();
  • 插入
$user = new UserModel();
$user->mobile = $mobile;
$user->password = $password;
$res = $user->save();
$id = $user->userid
  • 更新
$user = new UserModel();
$user->save(['name'=>'haha'],['userid'=>$userid]);
  • 对象转数组
$list = User::all();
if($list) {
    $list = collection($list)->toArray();
}
  • 新增属性

新增属性必须得赋值对象

$user->append(['code']);
$user->code = 0;
  • 读取器

能够获取一个不存在的字段,若是触发这个不存在的字段可使用append()路由

public function getCompanyNameAttr($value, $data) {
    if (array_key_exists('company_id', $data)) {
        $company_id = $data['company_id'];
        return UserModel::where('userid',$company_id)->value('name');
    }
    return '';
}

$output->append(['company_name','code','msg']);
相关文章
相关标签/搜索