How to put calculated fields in a data provider

Hello friends, I wanted to ask you something very simple;

You are setting up a data provider for my GridView as follows. As you can see, I put a calculated field “count(talleresdet_id) as cantidad” in the Select statement:

           echo GridView::widget([
    'dataProvider' =>new \yii\data\ActiveDataProvider([
        'query'=> StaVwCitas::find()->select([
            ' count(talleresdet_id) as cantidad','codtra' , 'codfac' ,
 'aptutor','nombrestutor','proceso'])->andWhere( .... ]

The problem is that when the specific this field name, in the columns property of the grid,
‘codtra’, …

I get error :
The StaVwCitas has not property “cantidad”

Can you Help me?

I don’t see groupBy(…) in your extract, and if you use yii debug the sql query should be visible and could be checked against the database server.

There are some suggestions about using groupBy() is the documentation that may provide some clues. API docs for db active record

The Sql syntax is correct, only I have not placed it complete and I have already tested it with createCommand () -> geRawSql ()

The problem is that I want to put the calculated field as a column in the GridView, but I get an error. If I use ArrayDataProvider it works but I prefer to use ActiveDataProvider

Hi @Geus,

Just look at the section of the guide that @mbunyan suggests and you’ll be OK.
Guide > Active Record > Selecting extra fields

In short, you can define an attribute in an Active Record for a calculated result.

1 Like