1

下面是我在 UserSearch 模型的搜索功能中使用的 dataprovider 查询,从 User 模型扩展而来。我试图在网格视图中显示“条目”的值,但无法访问“条目”列。请帮忙。

     $query = User::find()
         ->select("users.Id, users.FirstName, users.LastName, users.EntryId, GROUP_CONCAT(entry.EntryId ORDER BY entry.Id SEPARATOR '<br>') as entries")        
         ->join('LEFT OUTER JOIN','entry','entry.CustomerEntryId = users.EntryId')
         ->where(['users.IsActive' =>1])
         ->groupBy('users.Id');

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

用户型号代码

class User extends \yii\db\ActiveRecord
{
   public $password_repeat;
    public static function tableName()
    {
       return 'users';
    }

     public function rules()
    {
        return [
            [['FirstName','LastName','EmailId','MobileNo','Password','password_repeat'], 'required'],
            ['EmailId', 'email'],
            [['EmailId'], 'unique'],
            ['password_repeat', 'compare', 'compareAttribute'=>'Password', 'message'=>"Passwords don't match",  ],
            ['MobileNo', 'is8NumbersOnly'],         
        ];
    }

    public function is8NumbersOnly($attribute)
    {
        if (!preg_match('/^[0-9]{8}$/', $this->$attribute)) {
            $this->addError($attribute, 'must contain exactly 8 digits.');
        }
    }
}

网格视图代码

echo GridView::widget([
    'dataProvider'=>$dataProvider,
    'filterModel'=>$searchModel,
    'showPageSummary'=>true,
    'pjax'=>true,
    'striped'=>true,
    ],
    'panel'=>['type'=>'primary', 'heading'=>'Members Report'],
    'columns' => [

          [
            'label'=>'First Name',
            'attribute'=>'FirstName',
            'value'=>'FirstName',
          ],

          [
            'label'=>'Email Id',
            'attribute'=>'EmailId',
            'value'=>'EmailId',
          ],

          [
            'label'=>'Entries',
            'attribute'=>'Entries',
            'value'=>'entries',
          ],

    ],
]);       
4

1 回答 1

3

管理计算列的简单方法是在模型类中添加一个公共变量(在这种情况下为用户模型)

class User extends \yii\db\ActiveRecord
{
   public $entries

然后在您的gridview中,您可以简单地将此名称添加到列

'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        ....
        'entries',
        .....
于 2017-07-14T13:48:08.030 回答