在Yii2 Gridview中显示元素

时间:2015-11-22 11:00:51

标签: php ajax yii2 yii-extensions yii2-advanced-app

我试图在控制器的yii2视图动作中显示网格视图,但是我在非对象上得到了对成员函数getTotalCount()的调用错误

我有两个相关的表格,这些表格与表格Accusers有关,如下所示

案例模型coe:

    public function getAccusers()
{
    return $this->hasMany(Accuser::className(), ['case_ref_no' => 'ref_no']);
}

准确者模型关系

 public function getCaseRefNo()
{
    return $this->hasOne(Cases::className(), ['ref_no' => 'case_ref_no']);
}

在案例控制器中,动作索引具有网格视图,我在其中选择视图动作

在视图操作中,我已经通过了网格视图,基本上我希望它显示与某个案例相关的Accusers记录(就像上面的关系中的情况一样,参考不是​​这样)

案例控制器代码:操作视图

public function actionView($id)
{          
     $dataProvider = Accuser::find()
     ->where(['case_ref_no'=>$id])
    ->all();

    return $this->render('view', [
        'dataProvider' => $dataProvider,
    'model' => $this->findModel($id),
    ]);
}

视图页面(代码)

use kartik\grid\GridView;

...........

                 <?= GridView::widget([
                            'dataProvider' => $dataProvider,
                            'showPageSummary' =>false,
                            //'filterModel' => $searchModel,
                            'columns' => [
                                ['class' => 'yii\grid\SerialColumn'],


                                ['class' => 'yii\grid\ActionColumn'],
                            ],
                        ]); ?>

我用过“showPageSummary&#39; =&gt; false,但我仍然得到对非对象的成员函数getTotalCount()的调用错误

完整的原告模型

class Accuser extends \yii\db\ActiveRecord
{
/**
 * @inheritdoc
 */
public static function tableName()
{
    return 'accuser';
}

/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['fname', 'secname', 'date_of_birth', 'case_ref_no', 'idno'], 'required'],
        [['date_of_birth'], 'safe'],
        [['fname', 'secname', 'case_ref_no', 'idno'], 'string', 'max' => 100]
    ];
}

/**
 * @inheritdoc
 */
public function attributeLabels()
{
    return [
        'id' => 'ID',
        'fname' => 'Fname',
        'secname' => 'Secname',
        'date_of_birth' => 'Date Of Birth',
        'case_ref_no' => 'Case Ref No',
        'idno' => 'Idno',
    ];
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getCaseRefNo()
{
    return $this->hasOne(Cases::className(), ['ref_no' => 'case_ref_no']);
}

}

var_dump($ dataProvider);在我得到的视图中(在将find() - &gt; all()更改为仅查找()之后

  object(yii\db\ActiveQuery)[60]
   public 'sql' => null
   public 'on' => null
   public 'joinWith' => null
   public 'select' => null
   public 'selectOption' => null
   public 'distinct' => null
   public 'from' => null
   public 'groupBy' => null
   public 'join' => null
   public 'having' => null
   public 'union' => null
   public 'params' => 
   array (size=0)
    empty
   private '_events' (yii\base\Component) => 
     array (size=0)
     empty
   private '_behaviors' (yii\base\Component) => 
   array (size=0)
    empty
     public 'where' => 
       array (size=1)
     'case_ref_no' => string '#RERADRADAR' (length=11)
   public 'limit' => null
   public 'offset' => null
   public 'orderBy' => null
    public 'indexBy' => null
      public 'modelClass' => string 'frontend\models\Accuser' (length=23)
       public 'with' => null
        public 'asArray' => null
      public 'multiple' => null
     public 'primaryModel' => null
     public 'link' => null
     public 'via' => null
      public 'inverseOf' => null

2 个答案:

答案 0 :(得分:2)

对于dataProvider,您应该使用适当的类(ActiveDataprovider,ArrayDataProvider或SqlDataProvider)创建 这种方式并不像你做的那样直接形成查询

    $query = Accuser::find()
         ->where(['case_ref_no'=>$id]);

   $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

如果您认为问题与摘要有关,则应使用

避免此问题
 'summary'=>"", 

这样

<?= GridView::widget([

     'dataProvider' => $dataProvider,
     'summary'=>"", 

答案 1 :(得分:0)

将您的操作更改为此

public function actionView($id)
{          
    $dataProvider = Accuser::find()
    ->where(['case_ref_no'=>$id]);


   return $this->render('view', [
       'dataProvider' => $dataProvider,
   'model' => $this->findModel($id),
   ]);
}

gridview使用dataprovider并查找结果是您的数据提供者。如果你在使用all()后发现你的$ dataprovider变量是你的Accuser对象的数组!