如何在yii2 activerecord中连接两列?

时间:2017-10-07 11:45:11

标签: activerecord yii yii2

我是yii2(和yii)的新手。我有一个查询返回客户的名称和姓氏以及其他列。我使用activeDataProvider来显示表中的记录,但我需要在一个表列中显示名称和姓氏。

我想知道的是:

1. how do I concatenate the columns in yii2 activerecord?
2. How do I display the columns in one table columns made gridView?

以下是我尝试过的无效方法:

1. $query->select(['customer.*', 'fullname'=>concat('customer.name'," ",'customer.lastname')]);
2. 'dataProvider' => $model['dataProvider'],
                'columns' => [
                  'id',
                  'name'.' '.'lastname',
                  'customer_orders',
                  'created_at:datetime'

1 个答案:

答案 0 :(得分:2)

使用 GridView ,如下所示。

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        'id',
        [
           'attribute' => 'name',
           'value' => function ($model) {
               return $model->name . ' ' . $model->lastname;
           }
        ]
        'customer_orders',
        'created_at:datetime'
    ]
]) ?>

参考Yii2 GridView

相关问题