Error when trying to use CSqlDataProvider with CButtonColumn

Hi there,

I wanted to use code generated by Gii for displaying all results of a particular model (slightly modified by me, but still working without any problems):


$criteria = new CDbCriteria;

$criteria->select='ID, LOGN, MAIL';


$dataProvider = new CActiveDataProvider($model, array

(

    	'pagination'=>array('pageSize'=>20),

    	'sort'=>array('defaultOrder'=>'ID'),

    	'criteria'=>$criteria,

));


$this->widget('zii.widgets.grid.CGridView', array

(

    	'id'=>'users-grid',

    	'dataProvider'=>$dataProvider,

    	'filter'=>$model,

    	'columns'=>array

    	(

            	'ID',

            	'LOGN',

            	'MAIL',

            	array

            	(

                    	'class'=>'CButtonColumn',

            	),

),));

and to change it from using CActiveDataProvider to CSqlDataProvider. Therefore I pasted example code found in CSqlDataProvider in class reference with a little bit modifications from my side:


$count = Yii::app()->db->createCommand('SELECT COUNT(*) FROM WWW_USERS')->queryScalar();

$sql = 'SELECT * FROM WWW_USERS';


$dataProvider = new CSqlDataProvider($sql, array

(

 	'id'=>'user',

 	'keyField'=>'ID',

 	'totalItemCount'=>$count,

 	'sort'=>array

 	(

            	'attributes'=>array

            	(

                    	'ID', 'LOGN', 'MAIL',

            	),

            	'defaultOrder'=>'ID'

 	),

 	'pagination'=>array

 	(

            	'pageSize'=>20,

 	),

));

But I have to turn-off (comment-out) CButtonColumn column because with it enabled (first block of code) I’m getting error: “Trying to get property of non-object” with “CComponent.php(616) : eval()'d code:1”.

Since in both cases I used not my own code (first generated by Gii, second taken from guide), if I didn’t broke something during these slight customizations, then it seems to be an internal, core-code related problem. And I started this thread to ask, if someone has similar problems or maybe if someone sees an error in the code, I can’t find. Thank you.

Check the defaults for e.g. viewButtonUrl or deleteButtonUrl:The use $data->primaryKey since usually $data will be an object of type CActiveRecord. This is not the case when you use a CSqlDataProvider. So you need to do some adjustments here.

As error said: you trying to get property of non-boject, it means that one of your ID’, ‘LOGN’, 'MAIL not exist in this case in table, or is error in view, your maybe trying to get some $data->object witch is not in mysql schema

Thanks for the advice! Can we say that CButtonColumn is AR-oriented? I mean - I’m thinking if looking around CButtonColumn and trying to adapt it for CSqlDataProvider would not cost more time than simply resign of using in in favour of adding simple links in place of buttons? But I’ll take a look and decide. Thanks again.

Igor, thanks for your answer, but I think that Mike’s idea is closer to my problem as nothing change in DB schema or data between switching from AR (which worked) to SQL.

Hey Trejder…

did you solve your Problem?

I’m having the exact same problem right now!