Skip to content

Instantly share code, notes, and snippets.

@nucklearproject
Created September 14, 2012 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nucklearproject/3725549 to your computer and use it in GitHub Desktop.
Save nucklearproject/3725549 to your computer and use it in GitHub Desktop.
Usar un campo tipo ENUM como un select list en YII
/* Suponiendo que hemos creado nuestro CRUD. creamos un componente - Obviamente lo guardamos como ZHtml.php dentro de nuestra carpeta components, no hace falta inicializarlo si en nuestro main.php tenemos el autoload
'import'=>array(
'application.models.*',
'application.components.*',
),
.....
*/
<?php
class ZHtml extends CHtml
{
public static function enumItem($model,$attribute)
{
$attr=$attribute;
self::resolveName($model,$attr);
preg_match('/\((.*)\)/',$model->tableSchema->columns[$attr]->dbType,$matches);
foreach(explode(',', $matches[1]) as $value)
{
$value=str_replace("'",null,$value);
$values[$value]=Yii::t('enumItem',$value);
}
return $values;
}
public static function enumDropDownList($model, $attribute, $htmlOptions)
{
return CHtml::activeDropDownList( $model, $attribute,ZHtml::enumItem($model, $attribute), $htmlOptions);
}
}
/* Para usarlo simplemente vamos a nuestro _form.php y llamamos a la clase y metodo. */
Ahora:
<?php echo ZHtml::enumDropDownList( $model,'lista', array()); ?>
Antes:
<?php // echo $form->textField($model,'lista',array('size'=>4,'maxlength'=>4)); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment