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就可以完成一系列功能

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
ThinkPHP6 的模型运行机制主要是基于 ORM(对象关系映射)思想实现的,它将数据库中的数据表映射为一个个的模型类,通过模型类的方法来操作数据库中的数据,避免了直接操作数据库的复杂性,提高了代码的可维护性和可读性。 具体来说,ThinkPHP6 的模型运行机制包括以下几个方面: 1. 数据库连接:在模型类中,通过继承 \think\Model 类来实现数据库连接,可以在模型类中通过 $this->db() 方法来获取当前模型对应的数据库连接实例。 2. 数据表映射:在模型类中,通过定义 $table 属性来指定当前模型对应的数据表名称,也可以通过 $pk 属性来指定当前模型对应的数据表主键字段名称。 3. 查询构建器:在模型类中,通过 $this->db() 方法返回的数据表查询构建器对象来实现对数据表的各种操作,如查询、更新、删除等。 4. 数据关联:在模型类中,可以通过定义关联方法来实现不同数据表之间的关联,如一对一关联、一对多关联、多对多关联等。 5. 事件机制:在模型类中,可以通过定义事件方法来实现对模型操作的监听,如 beforeInsert、afterInsert、beforeUpdate、afterUpdate、beforeDelete、afterDelete 等事件。 总之,ThinkPHP6 的模型运行机制非常灵活,可以通过模型类来实现对数据表的各种操作,并且支持数据关联和事件机制,方便开发者进行快速开发

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安全天天学

你的鼓励是对我最大的鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值