Problem with CGridView columns when changing data provider

Hi there,

How can I get actual content of a cell to use it to customise CLinkColumn or to make CButtonColumn work with non-model data providers in CGridView?

I have a controller and view using CGridView along with CActiveDataProvider and it works perfectly. I then copied view code do another view (and controller) and the only thing, I changed was to switch data provider from CActiveDataProvider (Active Record) to CSqlDataProvider (pure SQL query).

The first thing I noticed was "Trying to get property of non-object" in the line of code declaring CButtonColumn:






If I’m not mistaken, this is because I’m using CSqlDataProvider and CButtonColumn seems to be looking for some model property, right? Then the question is how to use CButtonColumn, if not using model-related data provider?

Another problem is with CLinkColumn, which I’m trying to customize with its own contents, for example like that:








But this also dies with exactly the same error (“Trying to get property of non-object” in yii\base\CComponent.php(616) with eval()'d code(1)), which is also OK if I’m not mistaken, as CLinkColumn.labelExpression says that $data can be only used when data provider is a model.

So, second question is, that if this piece of code works:


for declaring simple CDataColumn, then how be able to use CLinkColumn in the same context, that is to have cell contents put into label or labelExpression and value of another cell (another column of current data row) - i.e. ID - to be put into url or urlExpression?

When you use CSqlDataProvider, return $data is array therefore

Change: $data->UDAT

to : $data[‘UDAT’]

Thanks a lot, that explains much and solves problem of using $data in column that is inside CGridView, which is using CSqlDataProvider.

However, there’s still an open question, how to use CButtonColumn in case like that? Am I missing something or in current Yii implementation CButtonColumn can’t be used with CGridView that is using CSqlDataProvider as data source?

Just a reminder that you have to escape your quote marks since they’re inside another quote. So when you use CSqlDataProvider or CArrayDataProvider this would be the syntax:


Because I had questions too about it I wrote a wiki that explains a lot of things about CSqlDataProvider and use in CGridview