HANDLE ENUM FIELD FROM DB WITH YII

hi how to handle enum type field with yii?




this is my code im my current works


function opcoes($table,$field,$select='')

{

        $valuesAR = array();

        $r = mysql_query("SHOW COLUMNS FROM `$table` LIKE '$field'") or die("Unable to retrieve field values " . mysql_errno());

        if (mysql_num_rows($r) == 1)

		{

                $l = mysql_fetch_assoc($r);

                $valuesAR = explode("','", preg_replace("/(?:enum|set)\('(.+?)'\)/","\\1", $l['Type']));

                $default = $l['Default'];

        }

		if($select) { $op .= "<option value='".$select."' selected>".htmlentities($select)."</option>";}

		if(!$select){ $op .= "<option value='".$default."'selected>".htmlentities($default)."</option>";}

		foreach ($valuesAR as $v) {

		    $op.= "<option value='".$v."'>".htmlentities($v)."</option>";

		}

return $op;

}




solution:

maby help any one




function opcoes($table,$field,$select='')

{

		$connection = Yii::app()->db;

        $valuesAR = array();

        $sql ="SHOW COLUMNS FROM `$table` LIKE '$field'";

		$command = $connection->createCommand($sql);

        $data = $command->queryAll();

        $l = $data[0][Type];

        $valuesAR = explode("','", preg_replace("/(?:enum|set)\('(.+?)'\)/","\\1", $data[0][Type]));

        $default = $data[0]['Default'];

		$r=array();

		foreach($valuesAR as $campo=>$valor)

		{

		$r[$valor]=$valor;

		}


return $r;

}



Thank a lot for this sample megabr, it work very well.

I have just 2 questions:

1/ the param ‘select’ is for what kind of use ?

2/ why you don’t put it member of a class kind “class MyClass extends CActiveRecord”

Regards,