Yii2 column in GridView is not displaying (not set)

4.4k views Asked by At

I am trying to combine 3 tables in the my ActiveRecord Search().

I have this table

employees
id
first_name

projects
id
name

project_assignment
id
employee_id
project_id
date_added
date_removed

I am using GridView to display all the employees in my database. Now, I wanted to display project where the employee is being assigned. So having said this, obviously we need these 3 tables to connect.

In my Employees.php Model, i have this code:

public function getproject_assignment()
{
    return $this->hasMany(ProjectAssignment::className(), ['employee_id' => 'id']);
}

In my ProjectAssignment.php model

/**
 * @return \yii\db\ActiveQuery
 */
public function getEmployee()
{
    return $this->hasOne(Employees::className(), ['id' => 'employee_id']);
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getProject()
{
    return $this->hasOne(Projects::className(), ['id' => 'project_id']);
}

In my EmployeeSearch.php

class EmployeesSearch extends Employees
{
    public $project_name;
    public function rules()
    {
        return [
            [['id', 'employment_status_id'], 'integer'],
            [['project_name','string_id', 'first_name', 'last_name', 'middle_name', 'gender', 'birth_date', 'civil_status', 'phone', 'address', 'zip', 'email', 'position', 'start_date', 'tin', 'philhealth', 'sss', 'hdmf', 'photo_location'], 'safe'],
        ];
    }

   public function search($params)
    {

        $query = Employees::find();

        $query->addSelect(['projects.name as project_name','employees.*']);
        $query->leftJoin('project_assignment','project_assignment.employee_id = employees.id');
        $query->leftJoin('projects','projects.id = project_assignment.project_id');

        $query->andWhere([
            'project_assignment.date_removed' => NULL
        ]);


        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => array('pageSize' => 20),
        ]);


        $this->load($params);


        if (!$this->validate()) {
            // uncomment the following line if you do not want to any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
        ]);

        $query->andWhere([
            'employees.deleted_at' => NULL
            // 'projects.deleted_at' => NULL
        ]);

        $query->andFilterWhere(['like', 'string_id', $this->string_id])
            ->andFilterWhere(['like', 'first_name', $this->first_name])
            ->andFilterWhere(['like', 'last_name', $this->last_name])
            ->andFilterWhere(['like', 'middle_name', $this->middle_name])
            ->andFilterWhere(['like', 'gender', $this->gender])
            ->andFilterWhere(['like', 'civil_status', $this->civil_status])
            ->andFilterWhere(['like', 'phone', $this->phone])
            ->andFilterWhere(['like', 'address', $this->address])
            ->andFilterWhere(['like', 'zip', $this->zip])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'position', $this->position])
            ->andFilterWhere(['like', 'tin', $this->tin])
            ->andFilterWhere(['like', 'philhealth', $this->philhealth])
            ->andFilterWhere(['like', 'sss', $this->sss])
            ->andFilterWhere(['like', 'hdmf', $this->hdmf])
            ->andFilterWhere(['like', 'photo_location', $this->photo_location]);

        return $dataProvider;
    } 

In my index.php (view file)

$gridColumns = [
    [
        'attribute' => 'Project',
        'value' => 'projects', //the value means that this is the value of the column
                                                 //the zip here is the get parameter
        'filter' => Html::activeDropDownList($searchModel, 'project_name', ArrayHelper::map(\app\models\Projects::find()->asArray()->all(), 'id', 'name'),['class'=>'form-control','prompt' => 'Select Project']),
    ],
    'first_name',
    'middle_name',
    'last_name',
    // 'position',
    [
        'attribute' => 'position',
        'value' => 'position',
        'filter' => Html::activeDropDownList($searchModel, 'position', ArrayHelper::map(Employees::find()->groupBy('position')->asArray()->all(), 'position', 'position'),['class'=>'form-control','prompt' => 'Select Position']),
    ],
    ['class' => 'yii\grid\ActionColumn',
    'template' => '{update} {delete}'],

];

                <?php echo
                    GridView::widget([
                    'dataProvider' => $dataProvider,
                    'filterModel' => $searchModel,
                    'columns' => $gridColumns,
                        'export' => [
                            'fontAwesome' => true,
                        ]
                    ]);
                ?>

The problem here is the Project column is not displaying anything. Here's a screenshot: enter image description here

When I checked on Yii Debuger, this sql query has been ran:

SELECT `projects`.`name` AS `project_name`, `employees`.* FROM `employees` LEFT JOIN `project_assignment` ON project_assignment.employee_id = employees.id LEFT JOIN `projects` ON projects.id = project_assignment.project_id WHERE (`project_assignment`.`date_removed` IS NULL) AND (`employees`.`deleted_at` IS NULL) LIMIT 20

This query is correct and what I was expecting. The only problem is that the Project column is not displaying as the screenshot that i showed you above.

I am not sure how to make this work. Been scratching my head for a couple of hours already.

Any help please. Thank You!

2

There are 2 answers

0
Tony On BEST ANSWER
'value' => 'projects', //the value means that this is the value of the column

Actually the value here can be string or closure. If it is a string then it means that a string representing the attribute name to be displayed in this column as documentation says. Try to use closure. In your case it will be like this:

'value' => function ($model, $key, $index, $column){
    return $model->project->name;
}
1
Avia Barsik On

for kartik\grid\GridView;

                'class' => 'kartik\grid\EditableColumn',
                'attribute'=>'myAttribute',
                'header' => 'myHeader',                                            
                'editableOptions' => [                         
                    'inputType' => \kartik\editable\Editable::INPUT_TEXT,
                    'valueIfNull' => '-',

/**
 * @var string the value to be displayed. If not set, this will default to the attribute value. If the attribute
 * value is null, then this will display the value as set in [[valueIfNull]].
 */
public $displayValue;