This commit is contained in:
wanyongkang
2020-10-13 09:38:55 +08:00
parent 517b026891
commit 7d7d8f09ad
5 changed files with 245 additions and 102 deletions

View File

@@ -1,5 +1,12 @@
<?php
/*
* @Author: your name
* @Date: 2020-09-30 17:32:46
* @LastEditTime: 2020-10-12 11:16:10
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /phptest/fastphp/base/Model.php
*/
namespace fastphp\base;
@@ -12,12 +19,12 @@ class Model extends Sql
public function __construct()
{
//获取数据库表名
if(!$this->table){
if (!$this->table) {
//获取模型类名称
$this->model = get_class($this);
//删除模型名最后的model
$this->model = substr($this->model,0,-5);
$this->model = substr($this->model, 0, -5);
//数据库表名与类名一致
$this->table = strtolower($this->model);
@@ -26,10 +33,11 @@ class Model extends Sql
/**
* 获取总数目
*
*
*/
public function getCount(){
return $this->field('count(1) as count')->fetch();
public function getCount($where = [])
{
return $this->field('count(1) as count')->where($where)->fetch();
}
/**
@@ -39,9 +47,10 @@ class Model extends Sql
* @param $limit = '100' 限制查询100条
* $limit = '2,100' 查询第二页 100条数据
*/
public function getListPage($fields = '*',$order = 'id desc', $page = '50')
public function getListPage($where = [],$fields = '*', $order = 'id desc', $limit = '50')
{
return $this->field($fields)->order($order)->limit($page)->fetchAll();
return $this->field($fields)->where($where)->order($order)->limit($limit)->fetchAll();
}
}
}

View File

@@ -1,6 +1,5 @@
<?php
namespace fastphp\db;
use \PDOStatement;
@@ -27,31 +26,32 @@ class Sql
* 多条件查询
* ['id'=>['<',100]]
*/
public function where($where){
public function where($where)
{
$this->param = [];
$this->filter = '';
if($where){
if ($where) {
$this->filter .= ' WHERE ';
if(is_string($where)){
$this->filter .= ' '.$where;
if (is_string($where)) {
$this->filter .= ' ' . $where;
} else {
$flag = true;
foreach ($where as $key => $value){
if($flag){
foreach ($where as $key => $value) {
if ($flag) {
$flag = false;
if(is_array($value)){
$this->filter .= ' `'.$key.'` '.$value[0].' :'.$key;
if (is_array($value)) {
$this->filter .= ' `' . $key . '` ' . $value[0] . ' :' . $key;
$this->param[$key] = $value[1];
} else {
$this->filter .= ' `'.$key.'` = :'.$key;
$this->filter .= ' `' . $key . '` = :' . $key;
$this->param[$key] = $value;
}
} else {
if(is_array($value)){
$this->filter .= ' AND `'.$key.'` '.$value[0].' :'.$key;
if (is_array($value)) {
$this->filter .= ' AND `' . $key . '` ' . $value[0] . ' :' . $key;
$this->param[$key] = $value[1];
} else {
$this->filter .= ' AND `'.$key.'` = :'.$key;
$this->filter .= ' AND `' . $key . '` = :' . $key;
$this->param[$key] = $value;
}
}
@@ -67,37 +67,39 @@ class Sql
* @return $this
* $order='id desc'
*/
public function order($order = 'id desc'){
if($order){
$this->filter .= ' ORDER BY '.$order.' ';
public function order($order = 'id desc')
{
if ($order) {
$this->filter .= ' ORDER BY ' . $order . ' ';
}
return $this;
}
/**
* group by
* @param array $order
* @return $this
* $group=['sex','name']
*/
public function group($group = []){
if($order){
public function group($group = [])
{
if ($order) {
$this->filter .= ' GROUP BY ';
$this->filter .= ' '.implode(' ,',$group).' ';
$this->filter .= ' ' . implode(' ,', $group) . ' ';
}
return $this;
}
/**
* 查询limit
* @param string
* @param string
* @return $this
* $limit = '100' 限制查询100条
* $limit = '2,100' 查询第二页 100条数据
*/
public function limit($limit = '100'){
$this->filter .= ' LIMIT '.$limit.' ';
public function limit($limit = '100')
{
$this->filter .= ' LIMIT ' . $limit . ' ';
return $this;
}
@@ -107,7 +109,8 @@ class Sql
* @return $this
* $field = 'id,count(1),sub(num)'
*/
public function field($field = '*'){
public function field($field = '*')
{
$this->field = $field;
return $this;
}
@@ -116,11 +119,12 @@ class Sql
* 查询所有
* @return mixed
*/
public function fetchAll(){
$sql = sprintf('SELECT %s FROM `%s` %s',$this->field,$this->table,$this->filter);
public function fetchAll()
{
$sql = sprintf('SELECT %s FROM `%s` %s', $this->field, $this->table, $this->filter);
$this->filter = '';
$sth = Db::pdo()->prepare($sql);
$sth = $this->formatParam($sth,$this->param);
$sth = $this->formatParam($sth, $this->param);
$sth->execute();
return $sth->fetchAll();
@@ -130,11 +134,12 @@ class Sql
* 查询一条
* @return mixed
*/
public function fetch(){
$sql = sprintf('SELECT %s FROM `%s` %s',$this->field,$this->table,$this->filter);
public function fetch()
{
$sql = sprintf('SELECT %s FROM `%s` %s', $this->field, $this->table, $this->filter);
$this->filter = '';
$sth = Db::pdo()->prepare($sql);
$sth = $this->formatParam($sth,$this->param);
$sth = $this->formatParam($sth, $this->param);
$sth->execute();
return $sth->fetch();
@@ -145,11 +150,12 @@ class Sql
* @param $id
* @return mixed
*/
public function delete(){
$sql = sprintf('DELETE FROM `%s` %s',$this->table,$this->filter);
public function delete()
{
$sql = sprintf('DELETE FROM `%s` %s', $this->table, $this->filter);
$this->filter = '';
$sth = Db::pdo()->prepare($sql);
$sth = $this->formatParam($sth,$this->param);
$sth = $this->formatParam($sth, $this->param);
$sth->execute();
return $sth->rowCount();
@@ -160,10 +166,11 @@ class Sql
* @param $data
* @return mixed
*/
public function add($data){
$sql = sprintf('INSERT INTO `%s` %s',$this->table,$this->formatInsert($data));
public function add($data)
{
$sql = sprintf('INSERT INTO `%s` %s', $this->table, $this->formatInsert($data));
$sth = Db::pdo()->prepare($sql);
$sth = $this->formatParam($sth,$data);
$sth = $this->formatParam($sth, $data);
$sth->execute();
return $sth->rowCount();
@@ -174,8 +181,9 @@ class Sql
* @param $data
* @return int
*/
public function addAll($data){
$sql = sprintf('INSERT INTO `%s` %s',$this->table,$this->formatInsertAll($data));
public function addAll($data)
{
$sql = sprintf('INSERT INTO `%s` %s', $this->table, $this->formatInsertAll($data));
$sth = Db::pdo()->prepare($sql);
$sth->execute();
@@ -187,12 +195,13 @@ class Sql
* @param $data
* @return mixed
*/
public function update($data){
$sql = sprintf('UPDATE `%s` SET %s %s',$this->table,$this->formatUpdate($data),$this->filter);
public function update($data)
{
$sql = sprintf('UPDATE `%s` SET %s %s', $this->table, $this->formatUpdate($data), $this->filter);
$this->filter = '';
$sth = Db::pdo()->prepare($sql);
$sth = $this->formatParam($sth,$data);
$sth = $this->formatParam($sth,$this->param);
$sth = $this->formatParam($sth, $data);
$sth = $this->formatParam($sth, $this->param);
$sth->execute();
return $sth->rowCount();
@@ -204,10 +213,11 @@ class Sql
* @param array $params
* @return PDOStatement
*/
public function formatParam(PDOStatement $sth,$params = []){
foreach ($params as $param=>&$value){
$param = is_int($param)?$param+1:':'.ltrim($param,':');
$sth->bindParam($param,$value);
public function formatParam(PDOStatement $sth, $params = [])
{
foreach ($params as $param => &$value) {
$param = is_int($param) ? $param + 1 : ':' . ltrim($param, ':');
$sth->bindParam($param, $value);
}
return $sth;
}
@@ -217,16 +227,17 @@ class Sql
* @param $data
* @return string
*/
private function formatInsert($data){
private function formatInsert($data)
{
$fields = [];
$names = [];
foreach ($data as $key=>$value) {
$fields[] = sprintf('`%s`',$key);
$names[] = sprintf(':%s',$key);
foreach ($data as $key => $value) {
$fields[] = sprintf('`%s`', $key);
$names[] = sprintf(':%s', $key);
}
$field = implode(',',$fields);
$name = implode(',',$names);
return sprintf('(%s) VALUES(%s)',$field,$name);
$field = implode(',', $fields);
$name = implode(',', $names);
return sprintf('(%s) VALUES(%s)', $field, $name);
}
/**
@@ -234,18 +245,19 @@ class Sql
* @param $data
* @return string
*/
private function formatInsertAll($data){
private function formatInsertAll($data)
{
$fields = [];
$values ='';
foreach ($data[0] as $key=>$value) {
$fields[] = sprintf('`%s`',$key);
$values = '';
foreach ($data[0] as $key => $value) {
$fields[] = sprintf('`%s`', $key);
}
foreach ($data as $k=>$v){
$values .= '(\''.implode('\',\'',$v).'\'),';
foreach ($data as $k => $v) {
$values .= '(\'' . implode('\',\'', $v) . '\'),';
}
$field = implode(',',$fields);
$values = rtrim($values,',');
return sprintf('(%s) VALUES %s',$field,$values);
$field = implode(',', $fields);
$values = rtrim($values, ',');
return sprintf('(%s) VALUES %s', $field, $values);
}
/**
@@ -253,12 +265,13 @@ class Sql
* @param $data
* @return string
*/
private function formatUpdate($data){
private function formatUpdate($data)
{
$fields = [];
foreach ($data as $key=>$value){
$fields[] = sprintf('`%s`=:%s',$key,$key);
foreach ($data as $key => $value) {
$fields[] = sprintf('`%s`=:%s', $key, $key);
$this->param[$key] = $value;
}
return implode(',',$fields);
return implode(',', $fields);
}
}
}