您的位置:首页 > 博客中心 > 数据库 >

yii 输出当前的sql语句

时间:2022-03-14 16:29

<?php

namespace app\controllers;

use yii\web\Controller;
use yii\data\Pagination;
use app\models\Country;

class CountryController extends Controller{
    
    public function actionIndex(){
        $query = Country::find();

        $pagination = new Pagination([
            ‘defaultPageSize‘ => 7,
            ‘totalCount‘ => $query->count(),
        ]);        
        $countries = $query->orderBy(‘name‘)
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();
        $commandQuery = clone $query;
        echo $commandQuery->createCommand()->getRawSql();//SELECT * FROM `country` ORDER BY `name` LIMIT 7
        return $this->render(‘index‘, [
            ‘countries‘ => $countries,
            ‘pagination‘ => $pagination,
        ]);
    }
}

 

热门排行

今日推荐

热门手游