Blogger Information
Blog 43
fans 4
comment 0
visits 18984
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
thinkphp联表查询/模型创建/模型配置
汇享科技
Original
516 people have browsed it

1.链表查询 join

  1. $ret = Db::table('user u')
  2. ->join('info i', 'u.id = i.owner')
  3. ->select()
  4. ->toarray();
  5. print_r($ret);

2. 模型

模型的访问数据库方式:可以直接使用类名+::进行访问数据库操作的方法

  1. public function getUser()
  2. {
  3. $ret = User::where('id', '>', '3')->select();
  4. return $ret;
  5. }

在控制器内访问需要先进行实例化

  1. public function index()
  2. {
  3. $user = new User();
  4. $data = [
  5. 'name' => '小编501',
  6. 'email' => 'xb1@qq.com',
  7. 'password' => 123456,
  8. 'reg_time' => time()
  9. ];
  10. print_r($user->create($data));
  11. }
  1. 模型创建

创建模型类如果在database.php文件内设置了表前缀只需要把表前缀后面的设置为模型文件的名字 例如 ly_user 这是表名 模型名默认应该是LyUser.php,如果设置了表前缀那么可以使用User.php

  1. 模型配置可以通过一些变量设置一些配置例如:

81358-26nop7z8qrzj.png

  1. //模型提供了一些配置属性
  2. //$disuse 这个是配置废弃字段 在查询的时候不会进行输出
  3. protected $disuse = [
  4. 'reg_time',
  5. ];
  6. // 设置主键的字段名 默认是id 比如你的自增主键是uid 这里的值就改为uid
  7. protected $pk = 'id';
  1. 模型的获取器和修改器
  1. //模型主要功能
  2. //1.获取器 命名方式 get + 字段名 + Attr
  3. public function getStatusAttr($v)
  4. {
  5. $arr = [0 => '禁用', 1 => '正常'];
  6. return $arr[$v];
  7. }
  8. // 2.修改器 命名方式 set + 字段名 + Attr
  9. public function setPasswordAttr($v, $a)
  10. {
  11. $v = sha1($v);
  12. return $v;
  13. }
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!