Yii Framework Forum: no $data object with CSqlDataProvider or CArrayDataProvider - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

no $data object with CSqlDataProvider or CArrayDataProvider Rate Topic: ***** 1 Votes

#1 User is offline   luoshiben 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 15-January 09

Posted 02 October 2010 - 05:22 PM

From what I can tell, it appears that the $data object is not being populated in CGridViews for use in CGridColumns when the data provider for the CGridView is a CArrayDataProvider or CSqlDataProvider. To test, I made a simple example where I attempt to use the $data object in the cssClassExpression property of a CGridColumn.

My CGridView code is:
$this->widget('zii.widgets.grid.CGridView', array(
	'dataProvider'=>$dataProvider,
	'columns'=>array(
		array(
			'name'=>'year',
			'cssClassExpression'=>'($data->year >= 2000)?\'current\':\'old\'',
		),
	),
));


I attempted to use three different data providers, all containing the same structure and data, like so:
// attempt 1
$dataProvider = new CActiveDataProvider('Vehicle');

// attempt 2
$dataProvider = new CSqlDataProvider('SELECT id, year FROM vehicle');

// attempt 3
$dataProvider = new CArrayDataProvider(array(array('id'=>1,'year'=>'1998'),array('id'=>2,'year'=>'1999')));


When I used the CActiveDataProvider the view rendered successfully and the expected class had been set on the table cell. However, when I tried with either the CArrayDataProvider or the CSqlDataProvider I get the error: "Error 500 Trying to get property of non-object".

Is this a bug, or is the $data object not supposed to be available when using CDataProviders other than the CActiveDataProvider? If this is the intended functionality, is there any way around this? I definitely need to be able to access the values from the current "row" for dynamic manipulation/styling of some data. Thanks!

Edit and Clarification: This applies to Yii 1.1.4.
0

#2 User is offline   luoshiben 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 15-January 09

Posted 06 October 2010 - 10:12 AM

After not getting any comments or replies on this issue, I finally posted it as a bug on the issue tracker. Qiang marked the bug as invalid with the response: "If you use array or sql provider, $data would be an array, instead of an object."

Ah! That makes sense! Too bad that wasn't mentioned (or I didn't figure it out) until after I had posted the bug. I tested it out and it worked like a charm. The new CGridView example would look like this:
$this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider'=>$dataProvider,
        'columns'=>array(
                array(
                        'name'=>'year',
                        'cssClassExpression'=>'($data["year"] >= 2000)?\'current\':\'old\'',
                ),
        ),
));


Thanks, Qiang!
2

#3 User is offline   Mike (UK) 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 01-August 10
  • Location:Exeter, UK

Posted 15 October 2010 - 01:49 AM

I have added a comment documenting the above to the CGridView reference page. There is an example there of how to code the buttons.
0

#4 User is offline   durik 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 16-December 10

Posted 13 January 2011 - 04:32 AM

 luoshiben, on 06 October 2010 - 10:12 AM, said:

The new CGridView example would look like this:
$this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider'=>$dataProvider,
        'columns'=>array(
                array(
                        'name'=>'year',
                        'cssClassExpression'=>'($data["year"] >= 2000)?\'current\':\'old\'',
                ),
        ),
));



I can't see the diference to the code you posted above, can you explain the difference please and would appreciate if you could post a link to your comment.

Anyway i appreciate ANYHELP that shows me how to use CArrayDataprovider instead of CActiveDataProvider.
0

#5 User is offline   luoshiben 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 15-January 09

Posted 19 January 2011 - 12:32 AM

 durik, on 13 January 2011 - 04:32 AM, said:

I can't see the diference to the code you posted above, can you explain the difference please and would appreciate if you could post a link to your comment.

Anyway i appreciate ANYHELP that shows me how to use CArrayDataprovider instead of CActiveDataProvider.


The difference is in how the $data variable is used within the CGridView instance (in the 'cssClassExpression' index of the columns array in this example). In the case where the dataProvider is a CActiveDataProvider, the $data variable is an object, and so members of that object are referenced with -> object notation, like $data->member. In the case where the dataProvider is a CArrayDataProvider or CSqlDataProvider, the $data variable is an array, so "members", or indices, of the array are accessed with bracket notation, like $data["member"]. Take a second look at the cssClassExpression index of the columns array in my two examples to see this in action.

Mike (UK)'s comment is at the bottom of the documentation page for CGridView, here: http://www.yiiframew...1/CGridView#c3.

Hope that helps. Good luck!
1

#6 User is offline   pamkiki 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 20-March 13

Posted 20 March 2013 - 01:31 PM

Excellent! works for me! thanks Luoshiben =)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users