Access $Data Array In Carraydataprovider


(Anthony Gough) #1

I am trying to access the data created from CArrayDataProvider - the view shows the data correctly as you need to access the data as an array rather than an object. The problem I am having is accessing the data in the ButtonColumn.

In the following view the dataProvider is a CArrayDataProvider.




$this->widget('bootstrap.widgets.TbGridView', array(

    'type' => 'striped bordered condensed',

    'dataProvider' => $dataProvider, 

    'ajaxUpdate'=>false, 

    'enablePagination'=>true,

    'summaryText'=>'Displaying {start}-{end} of {count} results.',

    'template' => "{summary}{items}{pager}",

    'htmlOptions'=>array('style' => 'width:500px;margin-left: 130px;',),

    'columns' => array(

        array('name' => 'name', 'header'=> 'Trading Name',

            'type'=>'raw',

            'value'=>'CHtml::encode($data["name"])',

            ),



That all works fine - the problem is accessing the data ButtonColumn. I have tried many permutations to try to get the data. The line of code that is the problem is




'url'=>'"/DCompliance/index.php?r=admin/view_detail_edit_smcontractor&id=".$data["pk"]."', //or tried

'url'=>'"/DCompliance/index.php?r=admin/view_detail_edit_smcontractor&id=$data[\'pk\'']"',



The button column at moment is




array(

            'class'=>'bootstrap.widgets.TbButtonColumn',

            ......

            'buttons'=>array

        (

        'view' => array

        (

           'label'=>'View Person',

           'imageUrl'=>Yii::app()->request->baseUrl.'/images/view.png',

           'url'=>'"/DCompliance/index.php?r=admin/view_detail_edit_smcontractor&id=$data[\"pk\"]"',

        ),   



How do I write the url to include the $data[‘pk’] of the array?


(Saurabhdhariwal737) #2

There’s difference in how the $data variable is used within the CGridView instance. 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->pk. In the case where the dataProvider is a CArrayDataProvider or CSqlDataProvider, the $data variable is an array, so indices of the array are accessed with bracket notation, like $data[“pk”].

For your problem, you need to change "url" option as below:


array(

        'class'=>'bootstrap.widgets.TbButtonColumn',

        ......

        'buttons'=>array

        (

                'view' => array

                (

                        'label'=>'View Person',

                        'imageUrl'=>Yii::app()->request->baseUrl.'/images/view.png',

                        'url'=>'Yii::app()->createAbsoluteUrl("admin/view_detail_edit_smcontractor", array("id"=>$data["pk"]))',

                ),

        )

)

For more details you can refer this URL.

Hope this helps you.

Thanks,

Saurabh Dhariwal