Yii Framework Forum: ENUM DB type in Yii - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

ENUM DB type in Yii Rate Topic: ***** 5 Votes

#1 User is offline   Kike 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 29-April 10

Posted 25 June 2010 - 09:05 AM

Hello all,

Whats the best practice in Yii to work with database fields that its values are a list of items?

Currently, I'm using the ENUM type of MySQL, but I don't know how Yii can work with this type. Is there any way to retrieve the values of the enum type and that Yii manages it?

Thanks in advance.

Regards.
Kike
0

#2 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 28 June 2010 - 03:00 AM

This is how I do:

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



Maybe you can do better with regExp, but this code at least works and gives you an array with the items.
2

#3 User is offline   Kike 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 29-April 10

Posted 01 July 2010 - 10:50 AM

Thank you very much, I'm afraid of the performance with this solution. Maybe it takes too much time?

Regards:
Kike
0

#4 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 02 July 2010 - 02:50 AM

I don't know.

I have never had problem of performance with this, but I never had more than 4-5 dropdownlist from enum in a page, I use only in form.

Just pay attention to reuse the items if you have to place 500 times in a page.
0

#5 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 02 July 2010 - 06:47 AM

I edited my previous post with a better one solution
0

#6 User is offline   Pampa 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 11-November 10
  • Location:Buenos Aires - Argentina

Posted 03 December 2010 - 12:21 AM

@Zaccaria

Hi! Nice solution.. I've seen others quite messed.

Sorry for my snoob question but I'm pretty new to Yii: ::)

Where it's supposed to be sit that function? Is there a place where it *must be* or where it could be *comfortable* with the rest of the framework?

TIA,
Pampa
0

#7 User is offline   Pampa 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 11-November 10
  • Location:Buenos Aires - Argentina

Posted 03 December 2010 - 12:44 AM

Ok, I can't wait! :lol:

I've dropped your code (commented out the line resolveName()) in my model file.

Then, in the view I've called it in this way:
<?php echo CHtml::activeDropDownList( $model,'status',$model->enumItem($model, 'status') ); ?>


Where 'status' is an ENUM('Active','Inactive','Deleted').

Hope to help someone :D
1

#8 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 03 December 2010 - 02:44 AM

*
POPULAR

I usally put this code in a class that I call ZHtml and put in compoment.

In this class there are my helpers. As they are static, they can be called on the class itself:

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

}


This allow you to write in the view (without any change in the model):
<?php echo CHtml::activeDropDownList( $model,'status',ZHtml::enumItem($model, 'status') ); ?>


or , more tasty:

<?php echo ZHtml::enumDropDownList( $model,'status'); ?>

14

#9 User is offline   fouss 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 385
  • Joined: 05-October 10
  • Location:Bamako Mali

Posted 14 December 2010 - 09:16 AM

Thanks zaccaria for that stuff, it helped me too.
suppose I have ENUM('Active','Inactive','Deleted')
How to get the value I saved in my text field when I want to modify the form?
If I select 'Inactive' and save then I noticed that 'Active' allways come first during any modification in the form.
Thanks!
Posted Image
0

#10 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 14 December 2010 - 10:23 AM

Simply use ve helper, you don't need any textfield.

Just write
<?php echo ZHtml::enumDropDownList( $model,'status'); ?>
and all will be fine.

If it is not saved the value, maybe is a problem of safe attributes.
0

#11 User is offline   fouss 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 385
  • Joined: 05-October 10
  • Location:Bamako Mali

Posted 14 December 2010 - 10:37 AM

Maybe I didn't notice something but I can see it's working now very well without any other modifications

That's a great stuff!

Thanks
Posted Image
0

#12 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 14 December 2010 - 02:08 PM

Yeahh!!

I was wondering what else could be wrong.. this piece of code has 2 years and alway did his work.

Happy that it helped.
1

#13 User is offline   Thearlie 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 26-February 11

Posted 13 June 2011 - 11:52 PM

Heres a little snippet to get it to work with Postgresql9 as well.

<?php
class ZHtml extends CHtml
{
        public static function enumItem($model,$attribute)
        {
		$values = array();
                $attr=$attribute;
                self::resolveName($model,$attr);
		if( $model->tableSchema instanceof CPgsqlTableSchema ) {
			$sql=<<<EOD
SELECT enumlabel FROM pg_enum
JOIN pg_type ON pg_type.oid = pg_enum.enumtypid
WHERE pg_type.typname = :table
EOD;
			$command=Yii::app()->db->createCommand($sql);
			$command->bindValue(':table',$model->tableSchema->columns[$attr]->dbType);

			$values=$command->queryColumn();
			
			Yii::trace( "Enum values were: ".print_r( $values, true ) );
		
		} else {
			if( preg_match('/\((.*)\)/',$model->tableSchema->columns[$attr]->dbType,$matches) > 0 ) {
				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 = array())
       {
          return CHtml::activeDropDownList( $model, $attribute,ZHtml::enumItem($model,  $attribute), $htmlOptions);
       
       
       }

}
?>

0

#14 User is offline   johnsnails 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 61
  • Joined: 05-September 11
  • Location:Sydney Australia

Posted 12 September 2011 - 08:22 PM

View Postzaccaria, on 03 December 2010 - 02:44 AM, said:

I usally put this code in a class that I call ZHtml and put in compoment.

In this class there are my helpers. As they are static, they can be called on the class itself:

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

}


This allow you to write in the view (without any change in the model):
<?php echo CHtml::activeDropDownList( $model,'status',ZHtml::enumItem($model, 'status') ); ?>


or , more tasty:

<?php echo ZHtml::enumDropDownList( $model,'status'); ?>


Hey,

Thanks for the concise instructions, I had to make one modification though, as enumDropDownList takes 3 arguments, the last one being $htmlOptions, i had to add array() as an argument, eg

<?php echo ZHtml::enumDropDownList($model,'status', array()); ?>


I'm pretty new to this so if that is dangerous, let me know.

Cheers!
0

#15 User is offline   Kevin Higgins 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 05-May 11

Posted 14 September 2011 - 10:03 AM

View Postjohnsnails, on 12 September 2011 - 08:22 PM, said:

Hey,

Thanks for the concise instructions, I had to make one modification though, as enumDropDownList takes 3 arguments, the last one being $htmlOptions, i had to add array() as an argument, eg

echo ZHtml::enumDropDownList( $model,'status', array());


I'm pretty new to this so if that is dangerous, let me know.

Cheers!


Generally, $htmlOptions is given a default argument $htmlOptions=array()

I changed it to the following when I used it:

public static function enumDropDownList($model, $attribute, $htmlOptions=array())
{
	return CHtml::activeDropDownList($model, $attribute, ZHtml::enumItem($model, $attribute), $htmlOptions);
}

1

#16 User is offline   jcsmesquita 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 27-July 11

Posted 21 October 2011 - 04:27 AM

View Postzaccaria, on 14 December 2010 - 02:08 PM, said:

Yeahh!!

I was wondering what else could be wrong.. this piece of code has 2 years and alway did his work.

Happy that it helped.


I've just implemented your code... Really great stuff, even after 2 years! Thanks!
0

#17 User is offline   huanito 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 21-November 10

Posted 27 January 2012 - 09:02 PM

View Postzaccaria, on 03 December 2010 - 02:44 AM, said:

I usally put this code in a class that I call ZHtml and put in compoment.
<snip />

great helper class Zaccaria. Thanks! :D

To set the selected item when using in update forms add this snippet, (replace 'attr' in 2 places with your model attribute):
<?php echo ZHtml::enumDropDownList($model, 'attr',
                 array(
                         'options'=>array('$model->attr'=>array('selected'=>true)),
                 ))
?>

0

#18 User is offline   pckabeer 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 19-May 12

Posted 20 May 2012 - 01:09 AM

thanks zachri , it helped me
0

#19 User is offline   pckabeer 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 19-May 12

Posted 25 May 2012 - 03:37 PM

A wiki Article on dropdownlist version of this code [enumDropdownlist](http://www.yiiframew...rate-into-giix/ "enumDropdownlist")
0

#20 User is offline   pckabeer 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 19-May 12

Posted 25 May 2012 - 03:38 PM

A wiki article on the RadioButtonlist version of this code [enumRadioButtonlist](http://www.yiiframew...rate-into-giix/ "enumRadioButtonlist")
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • 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