Cgridview $Data Variable

I have a CArrayDataProvider.




$movies=array(

        array(

            'id'=>1,

            'date'=>'...',

            'name'=>'...'

        ),

        array(

            'id'=>2,

            'name' => 'col3',

            'value' => '...',

        ),

        array(

            'id'=>3,

            'name' => 'col3',

            'value' => '...',

        ),

        

    );



And i created CArrayDataProvider below.




$dataProvider1= new CArrayDataProvider(

                    $movies, array(

                        'id'=>'id',

                        'keyField'=>'id',

                        'sort'=>array(

                            'attributes'=>array('id'),

                            'defaultOrder'=>array('id' => false),

                        ),

                       'pagination'=>array(

                          'pageSize'=>10, 

                       ),

                    )

            );



Here is CGridView.




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

                     'columns'=>array(

                                 array('name'=>'id', 'header'=>'#'),

                                 date,

                                 array('name'=>'name', 'header'=>'Name', value='CHtml::link($data->name, array("/movie/view", "id"=>$data->id))')

                         ),

            

        ));



I thought that variable $data represents a single element of $movies array. But there is error "Trying to get property of non-object ".

I want that the name column is link and when click on the link, it jumps to movie’s “view” view.

How do i do this? How do i use $data variable?

Does $data->id represents the attribute “id” of “movies” array’s one element?

You have to use “$data[‘id’]” and “$data[‘name’]” to access your data, because you are supplying an array of arrays to CArrayDataProvider.

If it had been an array of objects, then “$data->id” would have worked. :)

Thanks a lot

If you want to use each data rows, should be careful. You have to make sure the data set is not empty before you can use, otherwise, you will get "Error 500".




$dataProvider=$model->search();

$rows=$dataProvider->getData();

if(count($rows)>0)

{

    foreach($rows as $row)

    {

      ...

    }

}

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

	'id'=>'site-grid',

	'dataProvider'=>$dataProvider,

...

));



In this case, below ajax call may not update your data.




$('.search-form form').submit(function(){

	$('#site-grid').yiiGridView('update', {

		data: $(this).serialize()

	});

	return false;

});