Files
juipphp/fastphp/base/Model.php

105 lines
2.5 KiB
PHP
Raw Normal View History

2020-10-03 17:23:32 +08:00
<?php
2020-10-13 09:38:55 +08:00
/*
* @Author: your name
* @Date: 2020-09-30 17:32:46
2022-01-15 09:55:38 +08:00
* @LastEditTime: 2022-01-15 09:41:34
* @LastEditors: Please set LastEditors
2020-10-13 09:38:55 +08:00
* @Description: In User Settings Edit
* @FilePath: /phptest/fastphp/base/Model.php
*/
2020-10-03 17:23:32 +08:00
namespace fastphp\base;
use fastphp\db\Sql;
class Model extends Sql
{
protected $model;
public function __construct()
{
//获取数据库表名
2020-10-13 09:38:55 +08:00
if (!$this->table) {
2020-10-03 17:23:32 +08:00
//获取模型类名称
$this->model = get_class($this);
//删除模型名最后的model
2020-10-13 09:38:55 +08:00
$this->model = substr($this->model, 0, -5);
2020-10-03 17:23:32 +08:00
//数据库表名与类名一致
$this->table = strtolower($this->model);
}
}
2020-10-11 19:23:42 +08:00
/**
* 获取总数目
2020-10-13 09:38:55 +08:00
*
2020-10-11 19:23:42 +08:00
*/
2020-10-13 09:38:55 +08:00
public function getCount($where = [])
{
return $this->field('count(1) as count')->where($where)->fetch();
2020-10-11 19:23:42 +08:00
}
2020-11-17 10:18:30 +08:00
/**
* @description: 获取所有的列表
* @param {*}
* @return {*}
*/
public function getList($where = [],$fields = '*', $order = 'id desc')
{
return $this->field($fields)->where($where)->order($order)->fetchAll();
}
2020-10-11 19:23:42 +08:00
/**
* 按照页数获取数据
* @param $fields 'id,count(1)...'
* @param $order 'id desc'/'id asc'
* @param $limit = '100' 限制查询100条
* $limit = '2,100' 查询第二页 100条数据
*/
2020-10-14 20:20:49 +08:00
public function getListPage($where = [], $fields = '*', $order = 'id desc', $limit = '50')
2020-10-11 19:23:42 +08:00
{
2020-10-13 09:38:55 +08:00
return $this->field($fields)->where($where)->order($order)->limit($limit)->fetchAll();
2020-10-11 19:23:42 +08:00
}
2020-10-14 20:20:49 +08:00
/**
* @description: 获取一条数据
* @param {type}
* @return {type}
*/
public function getOne($where = [], $fields = '*')
{
2020-11-17 10:18:30 +08:00
return $this->field($fields)->where($where)->fetch();
2020-10-14 20:20:49 +08:00
}
2022-01-15 09:55:38 +08:00
/**
* @description: 获取一条数据
* @param {type}
* @return {type}
*/
public function getOneByStr($where = "", $fields = '*')
{
return $this->field($fields)->where($where)->fetch();
}
2020-11-17 10:18:30 +08:00
/**
* @description: 获取最新一条数据
* @param {*}
* @return {*}
*/
2020-11-20 16:01:10 +08:00
public function getNewOne($fields = '*',$where = [])
2020-11-17 10:18:30 +08:00
{
2020-11-20 16:01:10 +08:00
return $this->field($fields)->where($where)->order('id desc')->fetch();
}
2020-10-15 14:03:09 +08:00
/**
* @description: 更新一条数据
* @param {type}
* @return {type}
*/
public function updateOne($where = [], $data = [])
{
return $this->where($where)->update($data);
}
2020-10-13 09:38:55 +08:00
}