yii框架中的两表联查+分页

来源:互联网 发布:户型图平面设计软件 编辑:程序博客网 时间:2024/06/07 00:56

view视图部分

<?php
use yii\widgets\LinkPager;
foreach($models as $k=>$v){
    echo "<tr>";
    echo "<td>".$v['u_id']."</td>";
    echo "<td>".$v['u_name']."</td>";
    echo "<td>".$v['u_state']."</td>";
    echo "</tr>";
}
echo LinkPager::widget([
    'pagination' => $pages,
]);
?>


控制器部分页

<?php
use yii\data\Pagination;
public function actionList()
{
    $test=new TestForm();    //实例化model模型
    $arr=$test->find();
    //$countQuery = clone $arr;
    $pages = new Pagination([
        //'totalCount' => $countQuery->count(),
        'totalCount' => $arr->count(),
        'pageSize'   => 2   //每页显示条数
    ]);
    $models = $arr->offset($pages->offset)
        ->limit($pages->limit)
        ->all();
    return $this->render('list', [
        'models' => $models,
        'pages'  => $pages
    ]);
}
?>


控制器两表联查+分页  yii控制器中的方法

    public function actionHuihualist(){
        $cla_id=yii::$app->request->get('cla_id');
// $cla_id=3;//测试数据
        $test=new Huihua();    //实例化model模型
        $arr=$test->find();
        //$countQuery = clone $arr;
        $pages = new Pagination([
            //'totalCount' => $countQuery->count(),
            'totalCount' => $arr->count(),
            'pageSize'   => 2   //每页显示条数
        ]);
        $models = $arr->select('*')  //查询的对象指向
            ->innerJoin('student','huihua.h_stu_id=student.stu_id') //两表联查
            ->where(['h_cla_id'=>$cla_id]) //查询的条件
            ->offset($pages->offset)  //每页的偏移量
            ->limit($pages->limit)    //每页的条数
            ->asArray()               //转化成数组
            ->all();
        return $this->render('huihualist', [
            'data' => $models,  //把页面要循环的值传过去,data是名字,$models是查出来的值
            'pages'  => $pages,
            // 'data'=>$data
        ]);
    }

        //未分页的两表联查
        // $h=new Huihua();
        // $data=$h->find()->select('*')->innerJoin('student','huihua.h_stu_id=student.stu_id')->where(['h_cla_id'=>$cla_id])->asArray()->all();
        // return $this->render('huihualist',['data'=>$data]);



0 0
原创粉丝点击