网站首页 文章专栏 thinkphp model层常用方法汇总
<?php
namespace Home\Model;
use Think\Model;
class TableModel extends Model {
protected $autoCheckFields = false;
/* 获取数据表单条信息 */
public function getTableInfo($table='',$field='',$where=array()){
$model = M($table);
$info = $model->field($field)->where($where)->find();
return $info;
}
/* 添加或修改 数据表信息 */
public function addTable($table='',$where=array(),$data=array()){
$model = M($table);
if($where){
$info = $model->where($where)->save($data);
}else{
$info = $model->add($data);
}
return $info;
}
/* 列表 数据表信息 */
public function getTableList($table='',$field = '', $where = array(),$page=1,$pagesize=20, $order = ''){
$model = M($table);
$info = $model->field($field)->where($where)->page($page,$pagesize)->order($order)->select();
return $info;
}
/* 列表不分页 数据表信息*/
public function getTableLists($table='',$field = '',$where = array(), $order = ''){
$model = M($table);
$info = $model->field($field)->where($where)->order($order)->select();
return $info;
}
/* 字段加减*/
public function setTableNum($table='',$field='',$where=array(),$num=1,$type=false){
$model = M($table);
if($type){
$info = $model->where($where)->setInc($field,$num); // 加
}else{
$info = $model->where($where)->setDec($field,$num); // 减
}
return $info;
}
/* 查询加分组排序 */
public function getTableGroup($table='',$field='',$where=array(),$groupfield='',$page=1,$pagesize=20, $order = ''){
$model = M($table);
$info = $model->field($field)->where($where)->order($order)->group($groupfield)->page($page,$pagesize)->select();
return $info;
}
/* 计算总数 */
public function getTableCount($table='', $where = array(),$field='id'){
$model = M($table);
$info = $model->field($field)->where($where)->count($field);
return $info;
}
/* 计算字段值和 */
public function getTableSum($table = '', $where = array(), $field = 'id') {
$model = M($table);
$info = $model->field($field)->where($where)->sum($field);
return $info;
}
}
转载请注明出处