84 lines
2.0 KiB
PHP
84 lines
2.0 KiB
PHP
<?php
|
|
/*
|
|
* @Author: your name
|
|
* @Date: 2020-09-30 17:32:46
|
|
* @LastEditTime: 2020-10-23 16:23:12
|
|
* @LastEditors: kangkang
|
|
* @Description: In User Settings Edit
|
|
* @FilePath: /phptest/fastphp/base/Model.php
|
|
*/
|
|
|
|
namespace fastphp\base;
|
|
|
|
use fastphp\db\Sql;
|
|
|
|
class Model extends Sql
|
|
{
|
|
protected $model;
|
|
|
|
public function __construct()
|
|
{
|
|
//获取数据库表名
|
|
if (!$this->table) {
|
|
//获取模型类名称
|
|
$this->model = get_class($this);
|
|
|
|
//删除模型名最后的model
|
|
$this->model = substr($this->model, 0, -5);
|
|
|
|
//数据库表名与类名一致
|
|
$this->table = strtolower($this->model);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取总数目
|
|
*
|
|
*/
|
|
public function getCount($where = [])
|
|
{
|
|
return $this->field('count(1) as count')->where($where)->fetch();
|
|
}
|
|
|
|
/**
|
|
* 按照页数获取数据
|
|
* @param $fields 'id,count(1)...'
|
|
* @param $order 'id desc'/'id asc'
|
|
* @param $limit = '100' 限制查询100条
|
|
* $limit = '2,100' 查询第二页 100条数据
|
|
*/
|
|
public function getListPage($where = [], $fields = '*', $order = 'id desc', $limit = '50')
|
|
{
|
|
return $this->field($fields)->where($where)->order($order)->limit($limit)->fetchAll();
|
|
}
|
|
|
|
/**
|
|
* @description: 获取一条数据
|
|
* @param {type}
|
|
* @return {type}
|
|
*/
|
|
public function getOne($where = [], $fields = '*')
|
|
{
|
|
return $this->field($fields)->where($where)->limit('1')->fetch();
|
|
}
|
|
|
|
/**
|
|
* @description: 获取最新一条数据
|
|
* @param {*}
|
|
* @return {*}
|
|
*/
|
|
public function getNewOne($fields = '*'){
|
|
return $this->field($fields)->order('id desc')->limit(1)->fetch();
|
|
}
|
|
/**
|
|
* @description: 更新一条数据
|
|
* @param {type}
|
|
* @return {type}
|
|
*/
|
|
public function updateOne($where = [], $data = [])
|
|
{
|
|
return $this->where($where)->update($data);
|
|
}
|
|
|
|
}
|