Blogger Information
Blog 42
fans 0
comment 0
visits 15317
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0830 model创建、model查询数据库、model 的配置、model 获取器、修改器
小言
Original
387 people have browsed it

1.model创建

在App目录下新建model文件夹,然后在里面添加和你表明一样的文件。如果在数据库配置文件里面有配置过数据库表前缀,哪么新建的文件以大写开头,不带表前缀比如:User.php为例

2. model 的配置

在controller文件里的文件内引入model文件

  1. use app\model\User;

3.model查询数据库

  1. $ret = User::select();
  2. print_r($ret);

4. model 获取器、修改器

4.1获取器

获取器 格式 , get +字段 +Attr创建一个方法

  1. public function getStatusAttr($v){
  2. $arr = [
  3. 0 => '禁用',
  4. 1 => '开启',
  5. 2 => '待审核'
  6. ];
  7. return $arr[$v];
  8. }

4.1 修改器

修改器: set + 字段 + Attr 创建一个方法名

  1. public function inser($data){
  2. $ret = User::create($data);
  3. return $ret;
  4. }
  5. public function setPasswordAttr($v,$all){
  6. return md5($v);
  7. }
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!