yii2的多表关联和分页

Posted polax

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了yii2的多表关联和分页相关的知识,希望对你有一定的参考价值。

 /**
     * http://localhost/src/website/legal/admin-list
     *
     */
    public  function actionAdminList()
    {
        $page = parent::getPostParam ( ‘page‘, 1 ); // 当前页数
        \Yii::$app->params [‘dbSelect‘] = ‘main‘; // 切换到 main 数据库

        $query = CompanyModel::find()
            //->leftJoin(‘p2ps-1-jooyum.legal_company‘, ‘company.company_id = `p2ps-1-jooyum`.`legal_company`.company_id  AND `p2ps-1-jooyum`.`legal_company`.is_valid=1  ‘)
            ->leftJoin(‘p2ps-1-jooyum.legal_company_outsource  out1‘,‘ out1.`company_id`= `company`.`company_id` AND out1.class=1 ‘)
            ->leftJoin(‘p2ps-1-jooyum.legal_company_outsource  out2‘,‘ out2.`company_id`= `company`.`company_id`  AND out2.class=2 ‘)
            ->select([‘company.company_id‘,‘company.name‘,"COUNT(out1.`company_id`) AS ‘招商自然人数‘","COUNT(out2.`company_id`) AS ‘经销商数量‘"])
            ->groupBy(‘`company`.`company_id2`‘)
            ->where([‘company.is_valid‘=>VALID,‘company.status‘=>[1,2]]);


        $countQuery = clone $query;
        $pages = new Pagination([‘totalCount‘ => $countQuery->count()]);
        $list = $query->offset(($page-1)* \Yii::$app->params [‘webPageSize‘])
            ->limit( \Yii::$app->params [‘webPageSize‘])
            ->asArray()
            ->all();



        var_dump($list);

    }

 

以上是关于yii2的多表关联和分页的主要内容,如果未能解决你的问题,请参考以下文章

关于mybatis+sqlserver 的多表关联的分页(注意要先分页在多表关联)

yii2的多表查询

yii2多表关联案例代码

yii2 ActiveRecord多表关联以及多表关联搜索的实现

yii2 ActiveRecord多表关联以及多表关联搜索的实现

yii2 ActiveRecord多表关联以及多表关联搜索的实现