hi guys, i have in my database stored the number 0 or 1 for de gender but in the view zii.widgets.CDetailView it displays the value and i want to display the correct information that is female or male, how can i do this???
any help will be welcome.
hi guys, i have in my database stored the number 0 or 1 for de gender but in the view zii.widgets.CDetailView it displays the value and i want to display the correct information that is female or male, how can i do this???
any help will be welcome.
A quick and dirty sample:
<?php echo ($model->gender === 0) ? 'Female' : 'Male'; ?>
But maybe I would like to define constants for gender in the model class:
// model
class Info extends CActiveRecord
{
const FEMALE = 0;
const MALE = 1;
....
}
// view
<?php echo ($model->gender === Info::FEMALE) ? 'Female' : 'Male'; ?>
And I would add something more to the model class:
// model
class Info extends CActiveRecord
{
const FEMALE = 0;
const MALE = 1;
public static $genderLabels = array(
self::FEMALE => 'Female',
self::MALE => 'Male',
);
public function getGenderLabel()
{
return isset(self::$genderLabels[$this->gender]) ? self::$genderLabels[$this->gender] : '(undefined)';
}
....
}
// view
<?php echo $model->genderLabel; ?>
You can find another way of doing this in "Blog Tutorial".
Customizing Post Model > 4. Representing Status in Text
It’s more sophisticated and systematic.
thank you very much softark
i am pasting my zii.widgets.CDetailView for reference in the future
// view
<?php $this->widget(‘zii.widgets.CDetailView’, array(
'data'=>$model,
'attributes'=>array(
'id_prensa',
'tipo',
'pais',
'nombres',
'apellidos',
array(
'label'=>'Gender',
'value'=>$model->getGenderLabel()
),
'funcion',
'identificacion',
'lugar_pasaporte',
'fecha_nacimiento',
'medio',
'direccion',
'telefono',
'fax',
'email',
'equipo',
array(
'label'=>'Foto',
'type'=>'image',
'value'=>Yii::app()->baseUrl."/thumbs/".$model->foto,
),
'carta',
'documento',
'num_confirmacion',
'control',
'control_ip',
),
)); ?>
[size="4"]Hi I have perfect solution for OverWrite zii.widgets.CDetailView ( detail view ) data ,[/size]
follow the steps ,
step:1 prepare function on model file , to which select data from another table .
example like =>
public function getGenderFromId($id)
{
$WHERE_PART = 'G_ID='.$id;
$data = Yii::app()->db->createCommand()
->select('G_NAME')
->from('gender')
->where($WHERE_PART)
->queryAll();
return $data;
}
step : 2 In view file do code like =>
…
…
$genderInfo = $model->getGenderFromId($model->attributes[‘E_ID’]); // E_ID is id of person whose gender you have to find
$this->widget(‘zii.widgets.CDetailView’, array(
'data'=>$model,
'attributes'=>array(
'E_ID',
'E_NAME',
'E_EMAIL',
'E_PASSWORD',
array('label'=>'Gender','value'=>$genderInfo[0]['G_NAME']),
'E_PHONE1',
'E_PHONE2',
…
… this works 100%
[size="4"]enjoy.[/size]
…
Hi I have perfect solution for OverWrite zii.widgets.CDetailView ( detail view ) data ,
follow the steps ,
step:1 prepare function on model file , to which select data from another table .
example like =>
public function getGenderFromId($id)
{
$WHERE_PART = ‘G_ID=’.$id;
$data = Yii::app()->db->createCommand()
->select(‘G_NAME’)
->from(‘gender’)
->where($WHERE_PART)
->queryAll();
return $data;
}
step : 2 In view file do code like =>
…
…
$genderInfo = $model->getGenderFromId($model->attributes[‘E_ID’]); // E_ID is id of person whose gender you have to find
$this->widget(‘zii.widgets.CDetailView’, array(
‘data’=>$model,
‘attributes’=>array(
‘E_ID’,
‘E_NAME’,
‘E_EMAIL’,
‘E_PASSWORD’,
array(‘label’=>‘Gender’,‘value’=>$genderInfo[0][‘G_NAME’]),
‘E_PHONE1’,
‘E_PHONE2’,
…
… this works 100%
enjoy.
It’s an overkill to have a dedicated table for genders, because we only have 3 genders at most … MALE, FEMALE and UNKNOWN. I’d rather use constants or enums for them.
And for those items that can have a variety of values (e.g. country, city, language), we already have a nice and easy solution of Relational Active Record.
$this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'E_ID',
'E_NAME',
'E_EMAIL',
'E_PASSWORD',
array(
'label'=>'Country',
'value'=>$model->country->name,
),
'E_PHONE1',
'E_PHONE2',
....
for zii.widgets.CDetailViewarray
attributes'=>array(
'label'=>'Gender',
value'=>$model->getGenderLabel()),
works very well but haw can i call the getGenderLabel() function form zii.widgets.grid.CGridView?
and how it work with the search creteria… or maybe does anyone know if exist a tutorial that explains how to put a checkbox or a combobox directly in the grid
please any help will be welcome
Too many questions at a time But …
'value'=>'$data->getGenderLabel()',
I hope this will help you a little.
thank you softark, is very nice of you part to take the time and write down the solution to this trivial questions blessings. it works perfectly
Thanks Softark for sharing important hints…
1) call the getGenderLabel() function
'value'=>'$data->getGenderLabel()',