简体   繁体   English

Yii2-使用模型属性进行Gridview排序

[英]Yii2 - Gridview sorting using a model attribute

In my model, i have tableName set to a database. 在我的模型中,我将tableName设置为数据库。 I created a new attribute in the model called $fullname and defined it by joining with another table/model in my afterFind function like so: 我在模型中创建了一个名为$ fullname的新属性,并通过在afterFind函数中与另一个表/模型联接来定义它,如下所示:

public function afterFind()
{
    parent::afterFind();

    $temp = Yii::$app->db->createCommand("select last_name || ', ' || first_name as fullname from employees where EMP_ID = :emp_id");
    $temp->bindValues([':emp_id' => $this->EMP_ID]);
    $this->fullname = $temp->queryColumn()[0];
}

I am trying to figure out how I can use this model attribute in my gridview to sort by this fullname attribute. 我试图弄清楚如何在gridview中使用此model属性按此全名属性进行排序。

I have tried 我努力了

 $dataProvider->setSort([
    'attributes' => [
        'fullname' => [
            'asc' => ['fullname' => SORT_ASC],
            'desc' => ['fullname' => SORT_DESC],
            'label' => 'Full Name',
            'default' => SORT_ASC
        ]
    ]
    ]);

Can anyone tell me how I can sort by this attribute or an alternative to this solution? 谁能告诉我如何按此属性或此解决方案的替代方法进行排序?

You can use this : 您可以使用:

$dataProvider->sort->attributes['fullname'] = [
    'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC],
    'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC],
    'default' => SORT_ASC
];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM