Yii Framework Forum: HANDLE ENUM FIELD FROM DB WITH YII - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

HANDLE ENUM FIELD FROM DB WITH YII Rate Topic: -----

#1 User is offline   megabr 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 142
  • Joined: 23-March 09
  • Location:Brazil

Posted 07 January 2010 - 07:04 AM

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;
}


trying build code for Generate complete CRUD source code with YII... any help?
post comments here:
EXTENDING CRUD GENERATION (scaffolding)

old tentative released (module generator):
http://www.yiiframew...sion/modulegen/
0

#2 User is offline   megabr 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 142
  • Joined: 23-March 09
  • Location:Brazil

Posted 08 January 2010 - 08:21 AM

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;
}

trying build code for Generate complete CRUD source code with YII... any help?
post comments here:
EXTENDING CRUD GENERATION (scaffolding)

old tentative released (module generator):
http://www.yiiframew...sion/modulegen/
0

#3 User is offline   choiceerp 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 24-April 10
  • Location:EUROPA - France - Paris

Posted 15 May 2010 - 04:58 AM

View Postmegabr, on 08 January 2010 - 08:21 AM, said:

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,
knowledge is valid only if it is shared ;-)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users