PHP经验分享PHP实战

初识Yii2.0问题集(一)

2018-07-18  本文已影响5人  偏偏注定要落脚丶

  初学Yii框架,今天打算用Yii框架做一个简单的雇员信息的增删改查,结果步履维艰,每做一个功能都遇到问题。现把这些问题记录下来,希望给日后的学习提供一些帮助,如果有幸能得到志同道合朋友的指点更将感激不尽。(由于是初学,代码的任何地方都可能出问题。)


准备工作

显示雇员信息的的功能逻辑比较简单,先从控制器中获取所有的雇员信息,然后传给前端显示。

这里需要雇员模型雇员控制器显示雇员信息页面,文件如下:

<?php
namespace frontend\models;

use yii\db\ActiveRecord;

class Employee extends ActiveRecord
{
  public $name;
  public $number;
  public $gender;
  public $department;

  public static function tableName()
  {
      return 'employee'
  }
}

<?php
namespace 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
      ]);
  }

      .....
}
<?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.

还请懂的大神不吝赐教~~

上一篇下一篇

猜你喜欢

热点阅读