小诸葛
没有克隆对象, count()没用对
Controller action: function actionIndex() { $query = Article::find()->where(['status' => 1]); $countQuery = clone $query; $pages = new Pagination(['totalCount' => $countQuery->count()]); $models = $query->offset($pages->offset) ->limit($pages->limit) ->all(); return $this->render('index', [ 'models' => $models, 'pages' => $pages, ]); } View: foreach ($models as $model) { // display $model here } // display pagination echo LinkPager::widget([ 'pagination' => $pages, ]);
对比你的代码,你是怎么用的
- 小诸葛 回答了问题 在插入数据库之前处理字段?
public function beforeSave($insert) { if (parent::beforeSave($insert)) { // ...custom code here... return true; } else { return false; } }
- 小诸葛 回答了问题 yii2 url 美化后缀问题
'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ '<controller:\w+>/<action:\w+>'=>'<controller>/<action>', '<controller:\w+>/<action:\w+>.html'=>'<controller>/<action>', ] ],
测试过,
/site/about/
与/site/about.html
都可以访问 - 小诸葛 2016-04-15 已签到连续签到1天,获得了5个金钱
save()
之前判断一下$model->dirtyAttributes
是否有值,没有说明无变动, 有值再执行if save
- 小诸葛 回答了问题 yii2 url 美化后缀问题
suffix不要
rules加多一条'<controller:\w+>/<action:\w+>.html'=>'<controller>/<action>',
- 小诸葛 2016-04-13 已签到连续签到5天,获得了20个金钱
找不到该属性, 在model 定义一下
public $password;
- 小诸葛 回复了话题 yii的构造函数怎么用?
init,beforeAction 是什么鬼,明显的PHP语法错误,都没说到YII