Hello, this is my first post. I looked all over the internet but could not find a solution to this issue. The weird part is I have other grids from a many to many relationship that work just fine but this one is giving me a problem.
I have a many to many relationship between users and courses. One user can be join many courses and one course can have many users.
Here is my User model relationship…
/**
* @return \yii\db\ActiveQuery
*/
public function getCourses()
{
return $this->hasMany(Course::className(), ['id' => 'course_id'])->viaTable('course_user', ['user_id' => 'id']);
}
here is my Course model relationship
/**
* @return \yii\db\ActiveQuery
*/
public function getUsers()
{
return $this->hasMany(User::className(), ['id' => 'user_id'])->viaTable('course_user', ['course_id' => 'id']);
}
this is my controller action that create the dataprovider for the form…
public function actionUpdate($id)
{
//get course
$model = $this->findCourseModel($id);
//get users active query for dataprovider
$query = $model->getUsers();
//create dataprovider for gridview
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
else {
return $this->render('update', [
'model' => $model,
'dataProvider' => $dataProvider,
]);
}
}
and here is my gridview. The column that is having the problem is the ‘courses.name’ one…
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'courses.name',
'username',
'first_name',
'last_name',
'gender',
'status',
When I do a print_r on $query object I can see that I do have the Course name so the sql query gyazo.com/249aab9291cc23df85eca8721c20a170, but it shows up as "(not set)" on the gridview, gyazo.com/abe72ce4e2f6fbca330991d7dc0df1a2.
I have successfully done this same thing woth other many to many relationships but I can;t figure out why this doesn;t work. Can anyone point me in the right direction?
Thanks.