在 Yii2 Gridview 中显示元素
Displaying elements in Yii2 Gridview
我正在尝试在控制器的 yii2 视图操作中显示网格视图,但出现调用非对象上的成员函数 getTotalCount() 的错误
我有两个相关的 table 是 table 个与 table 原告相关的案例
案例模型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']);
}
在案例控制器中,动作索引有一个网格视图,其中我 select 视图动作
在视图操作中,我通过了网格视图,基本上我希望它显示与特定案例相关的原告记录(即案例编号,就像上面的关系一样)
案例控制器代码:动作视图
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,但我仍然收到 Call to a member function getTotalCount() on a non-object 的错误
完整的原告模型
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()
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
将您的操作更改为此
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() !
对于 dataProvider,您应该使用适当的 class(ActiveDataprovider、ArrayDataProvider 或 SqlDataProvider)创建
这样,而不是像您所做的那样直接形成查询
$query = Accuser::find()
->where(['case_ref_no'=>$id]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
如果您认为问题也与摘要有关,您应该避免使用
'summary'=>"",
这样
<?= GridView::widget([
'dataProvider' => $dataProvider,
'summary'=>"",
我正在尝试在控制器的 yii2 视图操作中显示网格视图,但出现调用非对象上的成员函数 getTotalCount() 的错误
我有两个相关的 table 是 table 个与 table 原告相关的案例
案例模型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']);
}
在案例控制器中,动作索引有一个网格视图,其中我 select 视图动作
在视图操作中,我通过了网格视图,基本上我希望它显示与特定案例相关的原告记录(即案例编号,就像上面的关系一样)
案例控制器代码:动作视图
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,但我仍然收到 Call to a member function getTotalCount() on a non-object 的错误
完整的原告模型
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()
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
将您的操作更改为此
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() !
对于 dataProvider,您应该使用适当的 class(ActiveDataprovider、ArrayDataProvider 或 SqlDataProvider)创建 这样,而不是像您所做的那样直接形成查询
$query = Accuser::find()
->where(['case_ref_no'=>$id]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
如果您认为问题也与摘要有关,您应该避免使用
'summary'=>"",
这样
<?= GridView::widget([
'dataProvider' => $dataProvider,
'summary'=>"",