ThinkPHP模型操作下

前言

model最重要的操作如下所示:


1. 模型设置

基本的模型设置,如下是最基本的模型设置:(模型设置就是在代码中进行设置,去覆盖掉我们在config进行的设置

在这里插入图片描述

模型的设置都是在类名中直接进行设置的

1.name(数据表除去前后缀的名字,默认是当前model的类名)

  • app/model/Cat.php
<?php
namespace app\model;

use think\model;

class Cat extends model
{
    
    
    protected $name = 'goods';
    public function findArr()   
    {
    
    
        $result = Cat::select();
        return $result;
    }
}

2.table(完整的数据表名)

  • app/model/Cat.php
<?php
namespace app\model;

use think\model;

class Cat extends model
{
    
    
    protected $table = 'shop_goods';
    public function findArr()   
    {
    
    
        $result = Cat::select();
        return $result;
    }
}

3.pk 改变主键名称

  • model 默认的主键是id
  • app/model/Cat.php
<?php
namespace app\model;

use think\model;

class Cat extends model
{
    
    
    protected $name = 'admin';
    protected $pk = 'uid';
    public function findArr()   
    {
    
    
        $result = Cat::select();
        return $result;
    }
}

4.schema 设置模型对应数据表字段及类型

  • 默认会自动获取(包括字段类型),但自动获取会导致增加一次查询
  • schema 属性一旦定义,就必须定义完整的数据表字段类型
  • 类型根据php数据类型定义,如果是json类型直接定义为json即可
  • app/model/Cat.php
class Goods extends Model{
    
    
    protected $name = 'Goods';
    protected $table = 'shop_goods';
    protected $pk = 'shop_id';
    protected $schema = [
        'shop_id' => 'int',
        'cat' => 'int',
        'title' => 'string',
        'price' => 'float',
        'discount' => 'int',
        'stock' => 'int',
        'status' => 'int',
        'add_time' => 'int'
    ];
    # 对某个字段定义需要自动转换的类型,可以使用type属性
    protected $type = [
        'shop_id' => 'int'
    ];
    public function select(){
    
    
        $select = Goods::select();
        return $select->toArray();
    }
}

5.disuse 数据表废弃字段(数组)

  • app/model/Cat.php
class Goods extends Model{
    
    
    protected $name = 'Goods';
    protected $table = 'shop_goods';
    protected $pk = 'shop_id';
    protected $disuse = [
        'discount',
        'stock'
    ];
    public function select(){
    
    
        $select = Goods::select();
        return $select->toArray();
    }
}

6.模型的其他属性

在这里插入图片描述

2. 模型的主要功能

1.获取器

  • 获取器的作用是对模型实例的(原始)数据做出自动处理
  • 命名规则:get + 字段名 + Attr
  • 字段名是数据表字段的驼峰转换
  • app/model/Goods.php
class Goods extends Model{
    
    
    public function index(){
    
    
        $find = Goods::find(10);
        echo $find->status;
        return $find->toArray();
    }
    public function getStatusAttr($v){
    
    
        $status = [
            1=>'开启',
            2=>'关闭'
        ];
        return $status[$v];
    }
}

2.修改器

  • 修改器的主要作用是对模型设置的数据对象值进行处理
  • 命名规则: set + 字段名 + Attr
  • app/model/Goods.php
class Goods extends Model{
    
    
    public function index(){
    
    
        $create = Goods::create([
            'cat' =>  3.33,
            'title' =>  '新商品',
            'price' =>  '59.99',
            'add_time' => time()
        ]);
        return $create;
    }
    public function setCatAttr($v,$all){
    
    
        // $all 全部参数
        return (int)$v;
    }
}

3. 搜索器

  • 搜索器的作用是用于封装字段(或者搜索标识)的查询条件表达式
  • 命名规则: search + 字段名 + Attr
  • app/model/Goods.php
class Goods extends Model{
    
    
    public function index(){
    
    
        $select = Goods::withSearch(['title'],[
            'title' => '新'
        ])->select();
        return $select->toArray();
    }
    public function searchTitleAttr($query,$v){
    
    
        $query->where('title','like', $v . '%');
    }
}

4. 检查数据

  • 如果要判断数据集是否为空,不能直接使用 empty 判断
  • 必须使用数据集对象的 isEmpty 方法判断
  • app/model/Goods.php
class Goods extends Model{
    
    
    public function index(){
    
    
        $select = Goods::where('title','1')->select();
        if(empty($select)){
    
    
            echo 111;
        }
        if($select->isEmpty()){
    
    
            echo 111;
        }
    }
}

3.模型事件

模型事件是指在模型被调用之前发生的一系列操作

在这里插入图片描述

namespace app\model;
use think\Model;
class Goods extends Model{
    
    
    public function one_update(){
    
    
        $update = Goods::update(
            ['price'=>'99.99'],
            ['id'=>22]
        );
        return $update;
    }
    # 执行更新操作,就会之下onBeforeUpdate方法
    public static function onBeforeUpdate($goods){
    
    
        print_r($goods->price);
        return true;
    }
}

总结

当要完成某一项功能的时候,就可以使用model,这样调用model就可以完成一系列功能

猜你喜欢

转载自blog.csdn.net/qq_53568983/article/details/128380893