php – GridView中的Yii2列未显示(未设置)

我想在我的ActiveRecord Search()中组合3个表.

我有这张桌子

employees
id
first_name

projects
id
name

project_assignment
id
employee_id
project_id
date_added
date_removed

我正在使用GridView显示我的数据库中的所有员工.
现在,我想显示分配员工的项目.所以说到这一点,显然我们需要这三个表来连接.

在我的Employees.php模型中,我有这样的代码:

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

在我的ProjectAssignment.php模型中

/**
 * @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']);
}

在我的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;
    } 

在我的index.php(查看文件)中

$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,
                        ]
                    ]);
                ?>

这里的问题是Project列没有显示任何内容.
这是一个截图:

当我检查Yii Debuger时,这个SQL查询已经运行:

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

这个查询是正确的,也是我所期待的.唯一的问题是Project列没有显示为我在上面显示的屏幕截图.

我不知道如何使这项工作.已经抓了几个小时了.

请帮忙.谢谢!

最佳答案

'value' => 'projects', //the value means that this is the value of the column

实际上这里的值可以是字符串或闭包.如果它是一个字符串,那么它意味着表示要在此列中显示的属性名称的字符串为documentation says.尝试使用闭包.在你的情况下,它将是这样的:

'value' => function ($model, $key, $index, $column){
    return $model->project->name;
}
点赞