Blogger Information
Blog 47
fans 0
comment 0
visits 21093
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
连表查询和model
P粉036614676
Original
405 people have browsed it

1.连表查询

使用join或者查询一次,再查询一次

  1. $res = Db::table('course c')->join('sc s','s.Cno' == 'c.Cno')->select()->toArray();
  2. print_r($res);
  3. $resute = Db::table('course')->column('Cno');
  4. $res = Db::table('course')->where('Cno','in',$resute)->select()->toArray();
  5. print_r($res);
  6. echo Request::method();
  7. print_r(Request::request());
  8. print_r(Request::server());

model

1.1model介绍

model要进行继承,继承基类model,use think\Model; 在基类model中封装了很多数据库操作方法

  1. class User extends Model
  2. {
  3. // protected $name
  4. public function list()
  5. {
  6. print_r(User::select()->toArray());
  7. }
  8. public function one()
  9. {
  10. //find要根据id来:
  11. print_r(User::find(0));
  12. }
  13. }

1.2model获取器

就是每一次操作的数据都会被获取器函数过滤一次,我们可以对数据进行更改
```php
public function getStatusAttr($v){
// 6.2、所有的状态都会传到这个方法里
// if($v == 1){
// $ret = ‘开启’;
// }else{
// $ret = ‘关闭’;
// }
// 这里要return 回去,给查询的那条语句
// return $ret;

  1. $arr = [
  2. 0=>'禁用',
  3. 1=>'启用',
  4. 2=>'待审核'
  5. ];
  6. return $arr[$v];
  7. // 直接返回数据,没有把 $v 这个变量使用上
  8. return ['0'=>'禁用','1'=>'启用',];
  9. }
  1. #### 1.2model修改器
  2. > 跟获取器大同小异
  3. ```php
  4. public function inser($data){
  5. // $ret = User::insert($data);
  6. $ret = User::create($data);
  7. // 注意:数据结果也是要返回,model里的任何对外的方法,都要用 return 返回值
  8. return $ret;
  9. }
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
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