2017-10-07 46 views
0

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

我想知道的是:

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' 

回答

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