Chicodes
Chicodes

Reputation: 23

yii2 display search filter and field from another table

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

Answers (2)

Chicodes
Chicodes

Reputation: 23

  1. in the studentsSearch class add department to the rules.

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'],
    ];

}

  1. 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'],
    ];
    
    
    ?>
    
  2. in students model inside your attributeLabels method add the departments.name attribute.

    public function attributeLabels() { 'Department.name' => 'Department Name' }

Upvotes: 0

e-frank
e-frank

Reputation: 749

read this: https://www.yiiframework.com/wiki/851/yii2-gridview-sorting-and-searching-with-a-junction-table-columnmany-to-many-relationship

  1. add a new property to your StudentsSearch, e.g. $departmentName
  2. add this property to StudentsSearch's rules, e.g. [['departmentName'], 'safe']
  3. use 'departmentName' in your query, e.g. $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

Related Questions