tp5.1,ThinkPHP5.1,thinkPHP5.1多表查询,联表查询和方法封装

/**
     * 公共查询数据方法
     * @param string $modelStr 模型名称(表名称)
     * @param $_where_order_field (条件)
     * @param bool $isReturnResult  是否返回结果
     * @param string $count (总数)
     * @return array
     */
    public function _getPageResultFromModel($modelStr, $_where_order_field, $isReturnResult = false, $count = 'count(*) as tp_count'){

        //定义变量
        $where  = array();$join   = array();
        $alias = $order = $group = $having = '';
        $field  = true;

        //变量赋值
        if (isset($_where_order_field['where'])) {
            $where = $_where_order_field['where'];
        }
        if (isset($_where_order_field['field'])) {
            $field = $_where_order_field['field'];
        }
        if (isset($_where_order_field['order'])) {
            $order = $_where_order_field['order'];
        }
        if (isset($_where_order_field['join'])) {
            $join = $_where_order_field['join'];
        }
        if (isset($_where_order_field['having'])) {
            $having = $_where_order_field['having'];
        }
        if (isset($_where_order_field['alias'])) {
            $alias = $_where_order_field['alias'];
        }
        if (isset($_where_order_field['group'])) {
            $group = $_where_order_field['group'];
        }

        //判断model的类型
        $model = $modelStr;
        if (is_string($model)) {
            $model = db($model);
        }
        //print_r($field);die;
        //获取总个数
        $count_ = $model->alias($alias)->where($where)->join($join)
            ->order($order)->having($having)->count();
//         print_r($count_);die;
        //总条数
        $sum =intval($count_);
        //当前页
        $currPage = input('page/d', 1);

        if (is_numeric($currPage) && $currPage == 0) {
            $currPage = 1;
        }
        //每页分页的行数
        $rows = input('rows/d', 20);
        //总页数
        $pages = ceil($sum / $rows);
        $limit = (($currPage - 1) * $rows) . ',' . $rows;

        //查询数据集合
        $lists = db($modelStr)->alias($alias)
            ->where($where)
            ->join($join)
            ->field($field)
            ->order($order)
            ->group($group)->having($having)
            ->limit($limit)
            ->select();
//return $model->getLastSql();
       // print_r($model->getLastSql());die;
        //die;

        //组装数据集
        $page_data = array(
            'totalPages'       => $sum,
            'sumpage'          => $pages,
            'pageindex'        => $currPage,
            'rowsize'          => $rows,
            'currPage'         => $currPage,
            'data'             => $lists,
        );

        if ($isReturnResult) {
            return $page_data;
        } else {
            echo json_encode($page_data);
        }
    }

 

       

/**
 * @authName 认证审核列表
 * @authStatus 1
 * @author zhangxinyu
 * @createTime 2018-10-23 18:08:52
 * @qqNumber 2639347794
 */
public function getUserCertificationList()
{

    $where = [];
    $keyword = trim(input('keyword/s'));//由当前当前关键字查出当前认证信息
    if (!empty($keyword)) {//添加关键字查询条件
        $where[] = ['u.mobile|u.realname', 'like', '%' . $keyword . '%'];
    }


    $_where_order_field['where'] = $where;
    //添加顺序倒序
    $sord = input('sord', 'desc');
    $sidx = input('sidx', 'u.id');
    $_where_order_field['order'] = 'u.'.$sidx . ' ' . $sord;
    //查询字段

    $_where_order_field['alias'] = "u";
    $_where_order_field['join'] = [

        ['yjy_developer_users d','u.user_id=d.id']
    ];
    $_where_order_field['field'] = "d.user_name,u.mobile";
    $companyList = $this->_getPageResultFromModel('users_certification', $_where_order_field, true);
    print_r($companyList);die;
    if (!empty($companyList)) {
        $data = $companyList['data'];
        foreach ($data as $index => $datum) {

            $data[$index]['create_time'] = date('Y-m-d H:i:s', $datum['create_time']);
        }

        $companyList['data'] = $data;

    }
    return $companyList;
}

 

如果我的内容对你有帮助,欢迎投食(dashang)。

你可能感兴趣的:(tp5.1,ThinkPHP5.1,thinkPHP5.1多表查询,联表查询和方法封装)