我有两个表“Personal_data”和“Students”,学生有Personal_data。 我用Gii来生成视图,控制器和模型的代码,我需要在GridView中显示来自“学生”的所有“Personal_data”,但我无法得到它。Yii2 - 显示两个表格之间的共同行
从模型代码/ 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,
]);
}
代码从视图/个人数据/ 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 Fatal Error – yii\base\ErrorException
Call to a member function andFilterWhere() on array
我怎样才能解决这个问题?
Bizley你是对的,删除all()方法,解决了错误,但GridView仍然显示“PersonalData”中的所有行。试了一遍又一遍我加了这个: $ query = PersonalData :: find() - > innerJoinWith('Students') - > where(['''','Students.id','null']] ); 现在我从“学生”处获得所有“personal_data”,谢谢。 –
为什么不以相反的方式显示关系?学生 - > personal_data。 – Bizley
我需要在PersonalData和其他学生中显示一些数据,但会是一个选项,谢谢。 –