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

标签 php yii yii2 cgridview

我正在尝试在我的 ActiveRecord Search() 中合并 3 个表。

我有这张 table

employees
id
first_name

projects
id
name

project_assignment
id
employee_id
project_id
date_added
date_removed

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

在我的 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,
                        ]
                    ]);
                ?>

这里的问题是项目列没有显示任何内容。 这是一个截图: enter image description here

当我检查 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

这个查询是正确的,符合我的预期。唯一的问题是项目列没有显示为我上面显示的屏幕截图。

我不确定如何进行这项工作。已经抓挠我的头几个小时了。

请帮忙。谢谢你!

最佳答案

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

其实这里的value可以是string或者closure。如果它是一个字符串 那么它意味着一个表示要在该列中显示的属性名称的字符串documentation says .尝试使用闭包。在您的情况下,它将是这样的:

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

关于php - GridView 中的 Yii2 列未显示(未设置),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30772270/

相关文章:

php - 在 PHP 的页面上多次调用单例方法不好吗?

php - 如何使用 PHP MySQl UPDATE 子句插入值?

php - 我动态地在多列中拆分单日的 Fullcalendar View

php - 创建一个以 API 为中心的应用程序,我自己的疑惑

php - 如何从 php 到 Android 通信

php - 从 json 中提取变量

yii2 - 将 bool 值设为 true 或 false 而不是 1 或 0

php - Yii2 - 命令 Controller 不工作 getalias webroot

yii - 如何配置 yiibooster

fonts - 如何在Yii2中的AppAsset中引入字体?