Error Displaying Boolean In Cgridview CGridView doesn't inform well 'Booleans', but CDetailView
Posted 13 December 2013 - 06:02 AM
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.
Posted 13 December 2013 - 01:38 PM
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".
Posted 13 December 2013 - 02:46 PM
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.