Expression in value of CGridView not working as expected

I am using a ternary operator as an expression in the value as seen in my view’s code below:


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

	'id'=>'trip-grid',

	'dataProvider'=>$dataProvider,

	// 'filter'=>$model,

	'columns'=>array(

    array(  'name'=>'f_user_admin',

						'header'=>'Admin?',

						'value'=>$data->f_user_admin,

        ),

    array(  'name'=>'f_user_admin',

						'header'=>'Admin?',

						'value'=>(($data->f_user_admin == 1) ? "Yes" : "No"),

        ),

		array(

			'class'=>'CButtonColumn',

        'viewButtonUrl'=>'Yii::app()->controller->createUrl("view",$data->primaryKey)',

        'updateButtonUrl'=>'Yii::app()->controller->createUrl("update",$data->primaryKey)',

        'deleteButtonUrl'=>'Yii::app()->controller->createUrl("delete",$data->primaryKey)',			

		),

	),

)); ?>



f_user_admin is a TINYINT(4) field in my mySQL database. The table has three records. f_user_admin has a value of 1 for the first record and a value of 2 for the other two records.

If you look at the attached image, you’ll see that the first column displays the correct values from the database. The seconds column should display “Yes” for a value of 1, and “No” for a value of 0. However, as you can see by the image, it always displays “No”.

Am I missing something?

Sorry, the file upload didn’t work, here it is.

The ternary part looks fine. I was going to suggest a datatype difference, but you’re using “==”, so you should be fine. What bothers me is the fact that both columns have the same name. The second column is really a computed field. Try changing the name and see if the same problem exists.

I tried changing the name as you suggested, but the problem persists. Before posting in the forum, I had also tried using gettype($data->f_user_admin) as the value, and that returns nothing, which probably means that it’s not recognizing a data type for the variable. This doesn’t make sense since, if I just put $data->f_user_admin as the value, it works (as it does in column 1 of the attached file in my second post). I also tried changing the data type in mySQL from tinyint(4) to int(11), just in case, but it didn’t work.

Anymore ideas?

This may sound like I’m grasping at straws, but what happens if you put single quotes around the expression?


'(($data->f_user_admin == 1) ? "Yes" : "No")',

I figured it out. There has to not be quotes around the expression, as such:


'value'=>'(($data->f_user_admin == 1) ? "Yes" : "No")',



When I started playing around with expressions last week, I did have quotes around the expression, and that worked great for CGridView. However, when I tried using the same format for an expression in a value for a CDetailView, it would print the expression, literally, on the screen. You can see my post on [post=‘91990’]that topic here[/post]. Someone suggested that I remove the quotes around the expression. I thought it worked at first, but only know am I realizing that it was not printing the correct value on the screen.

Anyway, this fixes the issue for the CGridView, but now I have to figure out how to get it to work for CDetailView. If you can help, let me know [post=‘91990’]here[/post].


 'value'=>'(($data->status_type == Pri) ? "Private" : "Public")', 

its working

Oh, thanx it works for me too. Actually you can even get rid of (( and ) signs.

Seems like there some difference between value of CGridView and value of CDetailView