您好我是YII的新手,我正在研究用户指南..在下一个例子中,当我打电话给
时,我不清楚$query = Country::find();
它从表中获取所有数据吗?现在当我在此之后使用时:
$countries = $query->orderBy('name')
->offset($pagination->offset)
->limit($pagination->limit)
->all();
它只是从第一个查询订购结果还是运行新查询? (查询是在这个代码中运行一次还是两次?)
<?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' => 5,
'totalCount' => $query->count(),
]);
$countries = $query->orderBy('name')
->offset($pagination->offset)
->limit($pagination->limit)
->all();
return $this->render('index', [
'countries' => $countries,
'pagination' => $pagination,
]);
}
}
答案 0 :(得分:0)
第一个问题是当你拨打$query->count()
时,那就是
SELECT COUNT(*) FROM `country`
第二个是当你致电$query->...->all()
时,会做类似
SELECT * FROM `country` LIMIT 0, 20