首先,先來看一下問題代碼:
1、控制器代碼如下:
????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, ????????]); ????}
(推薦教程:yii框架)
<?php return [ 'class' =>?'YIIdbConnection', ????'dsn'?=>?'mysql:host=localhost;dbname=yii2basic', ????'username'?=>?'root', ????'password'?=>?'', ????'charset'?=>?'utf8', ? ????//?Schema?cache?options?(for?production?environment) ????//'enableSchemaCache'?=>?true, ????//'schemaCacheDuration'?=>?60, ????//'schemaCache'?=>?'cache', ];
解決方法:
將pdo連接中的dsn的host由“localhost”改為“127.0.0.1”即可,打開文件DB.PHP,修改如下:
<?php return [ 'class' =>?'yiidbConnection', ????'dsn'?=>?'mysql:host=127.0.0.1;dbname=yii2basic', ????'username'?=>?'root', ????'password'?=>?'', ????'charset'?=>?'utf8', ? ????//?Schema?cache?options?(for?production?environment) ????//'enableSchemaCache'?=>?true, ????//'schemaCacheDuration'?=>?60, ????//'schemaCache'?=>?'cache', ];
更多編程相關內容,請關注php中文網yii框架欄目!
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END