我有两个表“Personal_data”和“Students”,学生有Personal_data。 我使用Gii为视图,控制器和模型生成代码,我需要在GridView中显示“Students”中的所有“Personal_data”,但我无法得到它。
来自models / PersonalDataSearch.php的代码
public function search($params)
{
$query = PersonalData::find()->joinWith('Students'])->all();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'document' => $this->document,
'telephone' => $this->telephone,
'direction' => $this->direction,
'sex' => $this->sex,
]);
$query->andFilterWhere(['like', 'names', $this->names])
->andFilterWhere(['like', 'lastNames', $this->lastNames])
->andFilterWhere(['like', 'email', $this->email])
->andFilterWhere(['like', 'movile', $this->movile]);
return $dataProvider;
}
来自controllers / PersonalDataController.php的代码
public function actionIndex()
{
$searchModel = new PersonalDataSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
来自views / personal-data / index.php的代码
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'names',
'lastNames',
'email:email',
'movile',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
此视图最初显示来自所有注册人的Personal_data表中的所有行,但我想只显示来自学生的Personal_Data,我添加了这一行:
$query = PersonalData::find()->joinWith('Students'])->all();
我有这个错误:
PHP致命错误 - yii \ base \ ErrorException
调用数组
上的成员函数andFilterWhere()
我该如何解决这个问题?
答案 0 :(得分:1)
从
中删除->all()
$query = PersonalData::find()->joinWith('Students'])->all();
数据提供程序负责从数据库中获取数据,您只需构建查询(all()
正在获取数据)。只需一个查询,您就可以向其添加过滤器,以便andFilterWhere()