Problem With CGridView

Hi All,

I am trying to use Zii widgets and I must say that using them saves a lot of effort .But I am stuck in a strange problem this time.

I have a custom function in my model that returns a string from my database on behalf of id.

I have successfully used it in CDetailView

<?php

$this->widget(‘zii.widgets.CDetailView’, array(

   'data'=&gt;&#036;model,


   'attributes'=&gt;array(


           'strDivisionName',


           array(                


                   'label'=&gt;&#036;model-&gt;getAttributeLabel('intZone_Id'),


                   'value' =&gt;&#036;model-&gt;ZoneName,


           )


   ),


   'htmlOptions'=&gt;array('class'=&gt;'question-answer'),

)); ?>

and it is working fine

It shows the Zone Name in place of Zone id(This is the functionality which I need)

I am trying to do the same with CgridView

<?php

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

   'id'=&gt;'division-grid',


   'dataProvider'=&gt;&#036;model-&gt;search(),


   


   'columns'=&gt;array(


           'strDivisionName',


          array(                


                   'name'=&gt;&#036;model-&gt;getAttributeLabel('intZone_Id'),


                   'type'=&gt;'raw',


                   'value'=&gt;&#036;model-&gt;ZoneName


           ),


           array(


                   'class'=&gt;'CButtonColumn',


                   'template'=&gt;'{view}{update}'


           ),


   ),

)); ?>

Here the label is coming up fine but value is not coming.It is coming as blank. I must be missing a very obvious point here.

Any help is highly appreciated…

Regards

You need to put it in your_model::search.

And in the rules function, make sure it’s set in the search scenario rule.

This should work better




<?php

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

  'id'=>'division-grid',

  'dataProvider'=>$model->search(),


  'columns'=>array(

    'strDivisionName',

    array(

      'name'=>$model->getAttributeLabel('intZone_Id'),

      'type'=>'raw',

      'value'=>'$data->ZoneName',

    ),

  ),

));

?>



/Tommy

Hi

Thanks a lot for paying attention to my problem.

If I use $data->ZoneName then its saying $data is undefined and as per my understanding it is giving correct error message as I havent defined anything as data.

This is the function I made in my model

public function getZoneName(){

           &#036;zoneId=&#036;this-&gt;intZone_Id;


           &#036;zoneName ='';


           if(trim(&#036;zoneId)&#33;=&quot;&quot; &amp;&amp; trim(&#036;zoneId)&#33;=NULL){


                   &#036;zoneNameObj  = Zone::model()-&gt;findbypk(&#036;zoneId,'',array('select'=&gt;'strZoneName'));        


                   &#036;zoneName =  &#036;zoneNameObj-&gt;strZoneName;


           }


           return &#036;zoneName;


   }

I have following search rules set in my model

public function search()

   {


           // Warning: Please modify the following code to remove attributes that


           // should not be searched.





           &#036;criteria=new CDbCriteria;





           &#036;criteria-&gt;compare('intDivision_Id',&#036;this-&gt;intDivision_Id);


           &#036;criteria-&gt;compare('strDivisionName',&#036;this-&gt;strDivisionName,true);


           &#036;criteria-&gt;compare('intZone_Id',&#036;this-&gt;intZone_Id,TRUE);





           return new CActiveDataProvider(get_class(&#036;this), array(


                   'criteria'=&gt;&#036;criteria,


           ));


   }

and in my controller actionadmin I have

function actionAdmin(){

$model=new Division(‘search’);

$model->unsetAttributes(); // clear any default values

if(isset($_GET[‘Division’]))

$model->attributes=$_GET[‘Division’];

$this->render(‘admin’,array(

'model'=&gt;&#036;model,

));

I hope I am a little more descriptive right now. Can u please guide me where am I going wrong

Regards

Thanks a Lot

It worked

I was missing single quotes ‘’ in my value field

Thnx all for paying attention

:rolleyes:

The $data variable exists locally when the CGridView rows are rendered (note the quotes around the ‘value’ value). I think you should include the zoneName property in a ‘select’ criteria when creating the dataprovider (not tested). See this example

(Please use [ code ] tags when providing code examples, for better readability. There’s a ‘<>’ button in the editor.)

/Tommy

Edit: Glad to hear you fixed it.