初学Yii框架,今天打算用Yii框架做一个简单的雇员信息的增删改查,结果步履维艰,每做一个功能都遇到问题。现把这些问题记录下来,希望给日后的学习提供一些帮助,如果有幸能得到志同道合朋友的指点更将感激不尽。(由于是初学,代码的任何地方都可能出问题。)
准备工作
显示雇员信息的的功能逻辑比较简单,先从控制器中获取所有的雇员信息,然后传给前端显示。
这里需要雇员模型、雇员控制器和显示雇员信息页面,文件如下:
Employee.php
在 \frontend\models\ 目录下创建Eemployee.php文件
<?phpnamespace frontend\models;use yii\db\ActiveRecord;class Employee extends ActiveRecord{ public $name; public $number; public $gender; public $department; public static function tableName() { return 'employee' } }
EmployeeController.php
在 \frontend\controllers\ 目录下创建EemployeeController.php文件
<?phpnamespace fronted\controllers;use frontend\models\Employee;use yii\web\Controller;use yii\data\ActiveDataProvider;class EmployeeController extends Controller{ public function actionIndex() { $model = new Employee(); $query = Employee::find()->all(); $dataProvider = new ActiveDataProvide([ 'query' => $query, 'pagination' => [ 'pageSize' =>10, ] ]); return $this->render('employeeList', [ 'model' => $model, 'dataProvider' => $dataProvider ]); } ..... }
EmployeeList.php文件
在frontend\views\employee目录下创建Employee.php文件
<?php use yii\grid\Gridview try{ echo GridView::widget([ 'dataProvider' => $dataProvider, 'columns' => [ ['class' => 'yii\grid\ActionColumn'], ], ]); } catch (Exception $e) { echo $e->getMessage(); }
这样的话就会报错:
The "query" property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses.
还请懂的大神不吝赐教~~
作者:偏偏注定要落脚丶
链接:https://www.jianshu.com/p/4163c2539710