在Yii2网格视图中显示元素


Displaying elements in Yii2 Gridview

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

我有两个相关的表格,即与下的Accusers表格相关的案例表格

案例模型coe:

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

Accusers模型关系

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

在控制器的情况下,动作索引有一个网格视图,我在其中选择视图动作

在视图操作中,我已经通过了网格视图,基本上我希望它显示与某个案件有关的原告的记录(这是案件参考号,与上面的关系一样)

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

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"=>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()->all()更改为find(()only 之后

  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

对于应该使用正确的类(ActiveDataprovider、ArrayDataProvider或SqlDataProvideer)创建的dataProvider这种方式,而不是像你做那样直接形成查询

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

如果你认为问题也与摘要有关,你应该使用来避免这种情况

 'summary'=>"", 

这边

<?= GridView::widget([
     'dataProvider' => $dataProvider,
     'summary'=>"", 

将您的操作更改为此

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

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

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