查询条件处理

This commit is contained in:
wanyongkang
2020-10-24 15:40:56 +08:00
parent 6390d6f8a7
commit 565a159745
4 changed files with 110 additions and 20 deletions

View File

@@ -2,7 +2,7 @@
/*
* @Author: your name
* @Date: 2020-09-30 17:32:46
* @LastEditTime: 2020-10-23 16:23:12
* @LastEditTime: 2020-10-24 12:11:18
* @LastEditors: kangkang
* @Description: In User Settings Edit
* @FilePath: /phptest/fastphp/base/Model.php
@@ -52,6 +52,18 @@ class Model extends Sql
return $this->field($fields)->where($where)->order($order)->limit($limit)->fetchAll();
}
/**
* 按照页数获取数据 复杂查询
* @param $fields 'id,count(1)...'
* @param $order 'id desc'/'id asc'
* @param $limit = '100' 限制查询100条
* $limit = '2,100' 查询第二页 100条数据
*/
public function getListPages($where = [], $fields = '*', $order = 'id desc', $limit = '50')
{
return $this->field($fields)->wheres($where)->order($order)->limit($limit)->fetchAll();
}
/**
* @description: 获取一条数据
* @param {type}

View File

@@ -31,9 +31,11 @@ class Sql
$this->param = [];
$this->filter = '';
if ($where) {
$this->filter .= ' WHERE ';
if(!strpos('$this->filter','WHERE') !== false){
$this->filter .= ' WHERE ';
}
if (is_string($where)) {
$this->filter .= ' ' . $where;
$this->filter .= ' WHERE ' . $where;
} else {
$flag = true;
foreach ($where as $key => $value) {
@@ -69,6 +71,73 @@ class Sql
return $this;
}
/**
* @description: 查询条件 处理复杂查询条件
* @param {*} [ 'day_time' => [ ['>' , val1], ['<' , val2] ] ]
* @return {*}
*/
public function wheres($where)
{
$this->param = [];
$this->filter = '';
if ($where) {
if(!strpos('$this->filter','WHERE') !== false){
$this->filter .= ' WHERE ';
}
if (is_string($where)) {
$this->filter .= ' WHERE ' . $where;
} else {
$flag = true;
foreach ($where as $key => $value) {
if ($flag) {
$flag = false;
if (is_array($value)) {
$flag2 = true;
foreach($value as $k => $v) {
if($flag2){
$flag2 = false;
if($v[0] == 'in'){
$this->filter .= ' `' . $key . '` ' . $v[0] . '('.implode(',',$v[1]).')';
} else {
$this->filter .= ' `' . $key . '` ' . $v[0] . ' :' . $key.$k;
$this->param[$key.$k] = $v[1];
}
} else {
if($v[0] == 'in'){
$this->filter .= ' AND `' . $key . '` ' . $v[0] . '('.implode(',',$v[1]).')';
} else {
$this->filter .= ' AND `' . $key . '` ' . $v[0] . ' :' . $key.$k;
$this->param[$key.$k] = $v[1];
}
}
}
} else {
$this->filter .= ' `' . $key . '` = :' . $key;
$this->param[$key] = $value;
}
} else {
if (is_array($value)) {
foreach($value as $k => $v) {
if($v[0] == 'in'){
$this->filter .= ' AND `' . $key . '` ' . $v[0] . '('.implode(',',$v[1]).')';
} else {
$this->filter .= ' AND `' . $key . '` ' . $v[0] . ' :' . $key.$k;
$this->param[$key.$k] = $v[1];
}
}
} else {
$this->filter .= ' AND `' . $key . '` = :' . $key;
$this->param[$key] = $value;
}
}
}
}
}
return $this;
}
/**
* 拼装排序条件
* @param array $order 排序条件