【发布时间】:2017-07-25 06:00:12
【问题描述】:
我有两个表“Personal_data”和“Students”,Student 有 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;
}
来自控制器/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
调用数组上的成员函数和FilterWhere()
我该如何解决这个问题?
【问题讨论】: