Reputation: 6629
Am trying to display a grid view in the yii2 view action of a controller but I get an error of Call to a member function getTotalCount() on a non-object
I have two related tables that is table Cases which is related to table Accusers as below
Case Model coe:
public function getAccusers()
{
return $this->hasMany(Accuser::className(), ['case_ref_no' => 'ref_no']);
}
Accusers model relation
public function getCaseRefNo()
{
return $this->hasOne(Cases::className(), ['ref_no' => 'case_ref_no']);
}
In the case controller the action index has a grid view in which I select the view action
In the view action I have passed grid view which basically I would wish that it displays the records of Accusers related to a certain case (That is the case ref no just as in the relationship above)
The case controller code: Action view
public function actionView($id)
{
$dataProvider = Accuser::find()
->where(['case_ref_no'=>$id])
->all();
return $this->render('view', [
'dataProvider' => $dataProvider,
'model' => $this->findModel($id),
]);
}
The view page (code)
use kartik\grid\GridView;
...........
<?= GridView::widget([
'dataProvider' => $dataProvider,
'showPageSummary' =>false,
//'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
I used 'showPageSummary' =>false, but I still get that error of Call to a member function getTotalCount() on a non-object
Full accuser model
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']);
}
}
The var_dump($dataProvider); in the view I got(after changing the find()->all() to 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
Upvotes: 0
Views: 3111
Reputation: 133360
For the dataProvider you should create with the proper class (ActiveDataprovider, ArrayDataProvider or SqlDataProvider ) this way and not directly form the query like you have done
$query = Accuser::find()
->where(['case_ref_no'=>$id]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
If you think the problem is related to the summary too you should avoid this using
'summary'=>"",
this way
<?= GridView::widget([
'dataProvider' => $dataProvider,
'summary'=>"",
Upvotes: 2
Reputation: 851
change your action to this
public function actionView($id)
{
$dataProvider = Accuser::find()
->where(['case_ref_no'=>$id]);
return $this->render('view', [
'dataProvider' => $dataProvider,
'model' => $this->findModel($id),
]);
}
gridview work with dataprovider and find result is your data provider . if you use all() after find your $dataprovider variable is an array of your Accuser object!
Upvotes: 0