yii2: Join tables not working in gridview
What steps will reproduce the problem?
Hello, I have a question. I have three tables in database users, userlanguage and language respectively. I want to show user language by getting it id from userlangauage table and take the name of language from language master.
It working proper when i only show the data of users table, because i am joining that two different tables(‘userlangauage’, ‘language’) in users model.
What is the expected result?
I want to show user’s language by getting languageID from userlanguage table and from that to get languagName from language table.
What do you get instead?
When i trying to render index.php than it does not render the data of join tables.
Additional info
modelsearch.php
$query ->select([
'user.userID',
'user.userEmail',
'languageName',
'l.languageName',
'user.userName',
'user.userMobile',
'user.userGender',
'user.userStatus',
])
->from('users user')
->leftjoin('userlanguage ul', 'user.userID = ul.userID')
->leftjoin('language l', 'ul.languageID = l.languageID');
model.php
public function getuser(){
return $this->hasOne(Users::className(), ['userID' => 'userID']);
}
public function getlanguages(){
return $this->hasOne(Language::className(), ['languageID' => 'languageID']);
}
// public function getuserslanguage(){
// return $this->hasOne(Userlanguage::className(), ['userID' => 'userID']);
// }
public function getuserslanguage() {
return $this->hasOne(Userlanguage::className(), ['userID' => 'userID'])->from(userlanguage::tableName() . ' FBF');
}
index.php
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'user.userEmail:email',
'l.languageID',
'user.userName',
'userMobile',
'userGender',
'l.languageName',
['class' => 'yii\grid\ActionColumn'],
]);
?>
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 29 (11 by maintainers)
@samdark , @softark I found the solution which i need. Follow this below link of Yii Forum http://www.yiiframework.com/wiki/851/yii2-gridview-sorting-and-searching-with-a-junction-table-column-many-to-many-relationship#add-comment