Yii Framework Forum: Error Displaying Boolean In Cgridview - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Error Displaying Boolean In Cgridview CGridView doesn't inform well 'Booleans', but CDetailView Rate Topic: -----

#1 User is offline   Totto Roncone 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 50
  • Joined: 12-September 13
  • Location:Il piacere - Europe

Posted 13 December 2013 - 06:02 AM

Hello!

I have two fields in a model that are Boolean datatype.

Strangely enough, CGridView displayed always as 'Yes', that is, as 'true' (it was autogenerated with Giix).

In my favour, saying that CDetailView shows the correct output: 'Yes' or 'No' depending on the numerical values ​​respectively '1' or '0' (true, false).



In my case, I use MySql database. The Booleans in MySql are built with TINYINT (1) and, in fact, Yii recognize correctly these, as Booleans, but not CGridView.


My question is: Is normal? Do I need to do some change?


Thanks in advance.
Thanks!
...Totto...
0

#2 User is offline   richmest 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 22-November 13
  • Location:New Jersey, USA

Posted 13 December 2013 - 01:38 PM

According to the documentation for the column property of Cgridview:
When a column is specified as a string, it should be in the format of "name:type:header", where "type" and "header" are optional. A CDataColumn instance will be created in this case, whose CDataColumn::name, CDataColumn::type and CDataColumn::header properties will be initialized accordingly.
The default for the type property is Text. Try specifying the type as "boolean".
1

#3 User is offline   Totto Roncone 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 50
  • Joined: 12-September 13
  • Location:Il piacere - Europe

Posted 13 December 2013 - 02:46 PM

View Postrichmest, on 13 December 2013 - 01:38 PM, said:

Try specifying the type as "boolean".


Amazing @richmest!

Hopefully, Giix auto-formats the CGridView columns, but get complicated much... and fails.

<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id' => 'language-grid',
	'dataProvider' => $model->search(),
	'filter' => $model,
	'columns' => array(
		'code',
		'name',
		array(
					'name' => 'active',
					'value' => '($data->active === 0) ? Yii::t(\'app\', \'No\') : Yii::t(\'app\', \'Yes\')',
					'filter' => array('0' => Yii::t('app', 'No'), '1' => Yii::t('app', 'Yes')),
					),
		...

... when simply must by define the column as boolean type (how well said @ richmest).

How? Enough with specify the name of the column thus: 'name_column: boolean', such as showed the following code.


<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id' => 'language-grid',
	'dataProvider' => $model->search(),
	'filter' => $model,
	'columns' => array(
		'code',
		'name',
                'active:boolean',

I hope, it is usefull to others.

Thanks!
...Totto...
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users