Created
September 14, 2012 23:15
-
-
Save nucklearproject/3725549 to your computer and use it in GitHub Desktop.
Usar un campo tipo ENUM como un select list en YII
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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
Fuente: http://www.yiiframework.com/forum/index.php/topic/10079-enum-db-type-in-yii/