# --------------------------------------------------------
# 数据库操作方法
# --------------------------------------------------------
/**
* 查询多条
* @param $model string 模型名或实例化的模型
* @param $where array|string 查询条件
* @param $field array|string 字段(默认为所有字段)
* @param $order string 排序(默认为空)
* @param $limit string 指定查询和操作的数量(传入'10'或者'5,10')
* @param $relation string 关联(默认不关联)
* @param $distinct boolean|string 唯一(去掉重复)
* @param $group boolean|string 分组
* @return $result array 结果集
* @ApiInternal
*/
public function getData($model = '', $where = true, $field = true, $order = null, $limit = false, $relation = false, $distinct = false, $group = false)
{
is_string($model) ? $model = Db::name($model) : null;
$field ? $model->field($field) : null;
$where ? $model->where($where) : null;
$order ? $model->order($order) : null;
$limit ? $model->limit($limit) : null;
$relation ? $model->relation($relation) : null;
$distinct ? $model->distinct($distinct) : null;
$group ? $model->group($group) : null;
$result = $model->select();
return $result;
}
/**
* 查询单条
* @param $model string 模型名或实例化的模型
* @param $where array/string 查询条件(默认为所有字段)
* @param $field array/string 字段(默认为所有字段)
* @param $order string 排序(默认为空)
* @param $relation string 关联(默认不关联)
* @return $result array 结果集
* @ApiInternal
*/
public function getOne($model = '', $where = true, $field = true, $order = null, $relation = false)
{
is_string($model) ? $model = Db::name($model) : null;
$field ? $model->field($field) : null;
$where ? $model->where($where) : null;
$order ? $model->order($order) : null;
$relation ? $model->relation($relation) : null;
$result = $model->find();
return $result;
}
/**
* 获取特定字段
* @param $model string 模型名或实例化的模型(不能为空)
* @param $where array/string 条件(不能为空)
* @param $field string 字段名(不能为空)
* @param $spea string 字段数据间隔符号 NULL返回数组
* @return $result array 删除记录数
* @ApiInternal
*/
public function getCol($model = '', $where = true, $field, $sepa = false)
{
is_string($model) ? $model = Db::name($model) : null;
if ($sepa == true) {
$result = $model->where($where)->column($field);
} else {
$result = $model->where($where)->value($field);
}
return $result;
}
/**
* 新增单条
* @param $model string 模型名或实例化的模型
* @param $data array 数据
* @return $result array 新增记录ID值
* @ApiInternal
*/
public function addOne($model = '', $data)
{
is_string($model) ? $model = Db::name($model) : null;
$result = $model->insertGetId($data, false, true);
return $result;
}
/**
* 新增多条
* @param $model string 模型名或实例化的模型
* @param $data array 数据
* @return $result array 新增记录最后一条ID值
* @ApiInternal
*/
public function addData($model = '', $data)
{
is_string($model) ? $model = Db::name($model) : null;
$result = $model->insertAll($data);
return $result;
}
/**
* 修改
* @param $model string 模型名或实例化的模型
* @param $where array/string 条件
* @param $data array 数据
* @return $result array 影响记录数
* @ApiInternal
*/
public function saveData($model = '', $where = true, $data)
{
is_string($model) ? $model = Db::name($model) : null;
$result = $model->where($where)->update($data);
return $result;
}
/**
* 更新字段值
* @param $model string 模型名或实例化的模型
* @param $where array/string 条件
* @param $data array 数据
* @return $result array 影响记录数
* @ApiInternal
*/
public function saveOne($model = '', $where = true, $data = '')
{
is_string($model) ? $model = Db::name($model) : null;
$result = $model->where($where)->setField($data);
return $result;
}
/**
* 自增、自减
* @param $model string 模型名或实例化的模型
* @param $where array/string 条件
* @param $data array 字段
* @param $type boolean 自增、自减
* @param $num int 数量
* @param $delay int/string 延时
* @return $result array 影响记录数
* @ApiInternal
*/
public function setIncDec($model = '', $where = true, $data = '', $type = true, $num = 1, $delay = '')
{
is_string($model) ? $model = Db::name($model) : null;
if ($type == true) {
$result = $model->where($where)->setInc($data, $num, $delay);
} else {
$result = $model->where($where)->setDec($data, $num, $delay);
}
return $result;
}
/**
* 删除
* @param $model string 模型名或实例化的模型
* @param $where array/string 条件(不能为空)
* @return $result array 删除记录数
* @ApiInternal
*/
public function delData($model = '', $where = true)
{
is_string($model) ? $model = Db::name($model) : null;
$result = $model->where($where)->delete();
return $result;
}
/**
* 统计总数
* @param $model string 模型名或实例化的模型
* @param $where array/string 查询条件
* @param $distinct boolean|string 唯一(去掉重复)
* @return $result array 记录数
* @ApiInternal
*/
public function countData($model = '', $where = true, $name = '*', $distinct = false)
{
is_string($model) ? $model = Db::name($model) : null;
$distinct ? $model->distinct($distinct) : null;
$result = $model->where($where)->count($name);
return $result;
}
/**
* 聚合查询
* @param $model string 模型名或实例化的模型
* @param $where array/string 查询条件
* @param $distinct boolean|string 唯一(去掉重复)
* @param $type int 1、最大;2、最小;3、平均;4、总和
* @return $result array 记录数
* @ApiInternal
*/
public function allTion($model = '', $where = true, $name = '*', $type = 1)
{
is_string($model) ? $model = Db::name($model) : null;
if ($type == 1) {
$result = $model->where($where)->max($name);
} elseif ($type == 2) {
$result = $model->where($where)->min($name);
} elseif ($type == 3) {
$result = $model->where($where)->avg($name);
} elseif ($type == 4) {
$result = $model->where($where)->sum($name);
} else {
$result = '参数错误';
}
return $result;
}
# --------------------------------------------------------
# 原生SQL查询 数据库操作方法
# --------------------------------------------------------
/**
* 原生SQL查询
* 注意query返回的是二维数组,返回单条第二个参数给个true
* @param $sql string 表名
* @param $type boolean 是否是查询、修改
* @ApiInternal
*/
public function runSql($sql = '', $type = false)
{
if ($type == true) {
//进行原生的SQL查询
$result = Db::query($sql);
} else {
$result = Db::execute($sql);
}
return $result;
}