Conditional in cGridView

I need to do some conditional logic in a cGridView passing in data parameters from the dataProvider. I read another post about adding a function to the model the cGridView is using (Posting), but I can’t figure it out.

I added a function called "getChecked" to the model being used (Rant), but I get the following error:

Property "Rant.getChecked" is not defined.

Below is my Rant Model code:




<?php


/**

 * This is the model class for table "Rant".

 *

 * The followings are the available columns in table 'Rant':

 * @property string $rantId

 * @property string $mp3

 * @property string $title

 * @property string $topicId

 * @property string $votes

 * @property string $voteValue

 * @property string $userId

 * @property string $length

 * @property string $phone

 * @property string $createDate

 */

class Rant extends CActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @return Rant the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'Rant';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('rantId, mp3, title, topicId, userId, length, phone, createDate', 'required'),

			array('rantId, topicId, votes, voteValue, userId, length', 'length', 'max'=>10),

			array('mp3', 'length', 'max'=>65),

			array('title', 'length', 'max'=>24),

			array('phone', 'length', 'max'=>14),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('rantId, mp3, title, topicId, votes, voteValue, userId, length, phone, createDate', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'user' => array(self::BELONGS_TO, 'User', 'userId'),

			'topic' => array(self::BELONGS_TO, 'Topic', 'topicId'),

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'rantId' => 'Rant',

			'mp3' => 'Mp3',

			'title' => 'Title',

			'topicId' => 'Topic',

			'votes' => 'Votes',

			'voteValue' => 'Vote Value',

			'userId' => 'User',

			'length' => 'Length',

			'phone' => 'Phone',

			'createDate' => 'Datetime',

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search()

	{

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

		// should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('rantId',$this->rantId,true);

		$criteria->compare('mp3',$this->mp3,true);

		$criteria->compare('title',$this->title,true);

		$criteria->compare('topicId',$this->topicId,true);

		$criteria->compare('votes',$this->votes,true);

		$criteria->compare('voteValue',$this->voteValue,true);

		$criteria->compare('userId',$this->userId,true);

		$criteria->compare('length',$this->length,true);

		$criteria->compare('phone',$this->phone,true);

		$criteria->compare('createDate',$this->createDate,true);


		return new CActiveDataProvider(get_class($this), array(

			'criteria'=>$criteria,

		));

	}

	

	/**

	 * Retrieves the last modified record in the Rant table.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function scopes($limit = 1)

    {

        return array(

            'recently'=>array(

                'order'=>'createDate DESC',

                'limit'=>$limit,

            ),

        );

	}

	

	public function getChecked()

	{

		return "anything";

	}

}



Below is my cGridView:




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

    'dataProvider'=>$dataProvider,

	'filter'=>$model,

    'columns'=>array(

        array(

			'name' => 'Date',

			'value'=> 'date("M j, Y g:ia", strtotime($data->createDate))',

		),

        'length', 

        'title',  

        array(            

            'name'=>'rating',

			'type'=>'raw',

			'value'=> 	'"<div class=\"starContainer\">

				<form id=\"$data->rantId\" action=\"\" method=\"post\">

					<input type=\"radio\" name=\"rate\" value=\"1\" title=\"Sucks\" $data->getChecked($data->votes, $data->voteValue) />

					<input type=\"radio\" name=\"rate\" value=\"2\" title=\"Fair\" />

					<input type=\"radio\" name=\"rate\" value=\"3\" title=\"Good\"  />

					<input type=\"radio\" name=\"rate\" value=\"4\" title=\"MindBlasting\"   />

					<input type=\"submit\" value=\"Rate it!\" />

				</form>

				<div id=\"loader$data->rantId\"><div style=\"padding-top: 5px;\">please wait...</div></div>

			</div>"',

        ),

        array(            

            'name'=>'User',

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

        ),

		array(            

            'name'=>'Listen',

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

        ),

    ),

));




As the error suggested, the property getChecked is not defined. What you created is a method (class function), not a property.

Maybe you can do it like this(untested)




...

<input type=\"radio\" name=\"rate\" value=\"1\" title=\"Sucks\" Rant::model()->getChecked($data->votes, $data->voteValue) />

...



or you can make your getChecked static:




public static function getChecked()

        {

                return "anything";

        }




then in your view:




...

<input type=\"radio\" name=\"rate\" value=\"1\" title=\"Sucks\" Rant::getChecked($data->votes, $data->voteValue) />

...



Again, these were not tested. I’m sure you’ll figure it out ;)

The problem turned out to be that cGridView evals the code passed into the “value”. My getChecked() function wasn’t called until I added a “.” operator. This is what I got to work:




array(            

            'name'=>'rating',

			'type'=>'raw',

			'value'=> 	'"<div class=\"starContainer\">

				<form id=\"$data->rantId\" action=\"\" method=\"post\">

					<input type=\"radio\" name=\"rate\" value=\"1\" title=\"Sucks\" ".Rant::getChecked($data->voteValue, $data->votes, 1)." />



Notice before Rant::getChecked() there is a quote and “.”. Hope this helps someone else as I didn’t see any examples on the forums explaining how to embed functions in cGridView values.

I knew you can figure it out! :)

Welcome to Yii forum BTW. :)