Reputation: 23
I have this in my controller departments class:
Hello guys
So there is something i am trying to achieve with yii
I have a Departments table and a student table. Inside the students view i want to show the departments name and the filter. While i am able to display the department name for each student but the department name filter doesn't show.
Below is my code with comments:
/*Begining of Students model*/
class Students extends \yii\db\ActiveRecord
/* this is where i did a reference to my students table*/
public function getDepartments()
{
return $this->hasMany(Departments::className(), ['department_id' => 'ID']);
}
--- End of Students model
--- Begining of Students View
/*Inside my view folder i have the Students folder where i have an index.php file where everything is listed in Gridview. */
View/Students/index.php
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\helpers\ArrayHelper;
use app\models\States;
use kartik\export\ExportMenu;
/* @var $this yii\web\View */
/* @var $searchModel app\models\AgentsSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Agents';
$this->params['breadcrumbs'][] = $this->title;
$gridColumns = [
['class' => 'yii\grid\SerialColumn'],
'created_at',
'name',
departments.name',
'status',
['class' => 'yii\grid\ActionColumn'],
];
// Renders a export dropdown menu
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns
]);
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'tableOptions' =>['class'=>'table table-bordered'],
'filterModel' => $searchModel,
'columns' => $gridColumns
]); ?>
</div>
/*End of Students view*/
/*Begining of Students search.*/
/*models/StudentsSearch.php*/
I understand that the code below inside my StudentsSearch.php file is responsible for displaying the filter for each field.
public function rules()
{
return [
[['phone'], 'integer'],
//my understanding is that this is what controls the filter form, only fields listed here have filters
[['phone','name' 'email', 'address','city','departments.name'], 'safe'],
];
}
/*End of Students search.*/
I am able to show filter for every other thing but how do i show filter for the department name?
Upvotes: 0
Views: 2777
Reputation: 23
Make reference to the Departments table by adding it to the rules.
public function rules()
{
return [
//this controls the filter section, only fields listed here will have filter
[['Department'], 'safe'],
];
}
Then in Students view in this case in my index.php i add the attribute "departments" and make reference to the deparment name using departments.name in my gridColumns
$gridColumns = [
['class' => 'yii\grid\SerialColumn'],
'name',
'email:email',
'address',
[
'attribute' => 'departments',
'label' => Department Name',
'value' => 'department.name'
],
'status',
['class' => 'yii\grid\ActionColumn'],
];
?>
in students model inside your attributeLabels method add the departments.name attribute.
public function attributeLabels() { 'Department.name' => 'Department Name' }
Upvotes: 0
Reputation: 749
StudentsSearch
, e.g. $departmentName
[['departmentName'], 'safe']
$query->andFilterWhere(['like', 'department.name', $this->departmentName])
sorting on a calculated field works similar, read this: https://www.yiiframework.com/wiki/621/filter-sort-by-calculatedrelated-fields-in-gridview-yii-2-0
$dataProvider->setSort([
'attributes' => [
'id',
'departmentName' => [
'asc' => ['department.name' => SORT_ASC],
'desc' => ['department.name' => SORT_DESC],
'label' => 'Department',
'default' => SORT_ASC
],
]
]);
Upvotes: 0