Yii 1.1: enum

Enum values in PHP - Yii
13 followers

Enum class for using enums in PHP - Yii framework.

Two type of enums can be instanced: regular Enums and DBEnums. The difference is that DBEnum enum values must exist in a database table (the ideal scenario for a DBEnum is an enumerated value in DB). A check against the database is performed before using DBEnum values. Take a look at the examples for further information.

Latest changes

  • Interface changed! Now it's simpler to call enum methods. WATCH OUT: this version is not compatible with previous Enums so you'll have to change Enum calls if you go with this version. The getEnum()-> part of the interface has been stripped out, now the interface looks like MyEnum::getValidValues().
  • Fixed a bug with radio button list
  • The newer version file is enums2.zip

Notes

If performance is a primary concern in you app, you should use the original version. Current version uses call_user_func_array which is slower than a direct call (not checked personally but according to some benchmarks it may be 12 times slower). I considered an acceptable trade off for the sake of a simpler interface.

Requirements

php >= 5.3.0 (PHP Reflection API required)

Usage

  • Declare an Enum: subclass Enum class and define the enum values as constants:
class MyEnum extends Enum
{
  const MY_ENUM_VALUE1 = "MY_VALUE_1";
  const MY_ENUM_VALUE2 = "MY_VALUE_2";
}
  • Declare a DBEnum: subclass DBEnum class and define the enum values as constants. Also define the two abstract methods used to access the DB:
class MyEnum extends DBEnum
{
  const MY_ENUM_VALUE1 = "MY_VALUE_1";
  const MY_ENUM_VALUE2 = "MY_VALUE_2";
 
  protected function getDBField()
  {
      return 'my_enum_id_field';
  }
 
  protected function getDBTable()
  {
      return 'myenum_table';
  }
 
  // Optionally define a condition if only some values of 
  //the table are to be taken into consideration
  /*
  protected function getDBCondition()
  {
      return "other_field=value";
  }*/
 
}

The rest of the methods apply to both Enums and DBEnums

  • I18N: Enum supports i18n through the Yii API. Enum translations are taken from a category called enums so to define your own translations just create a file enums.php file under messages/<locale_id> and write your translations like this:
return array(
    (...)
    MyEnum::MY_ENUM_VALUE1 => 'My value 1',
    MyEnum::MY_ENUM_VALUE2 => 'My value 2',
    (...)
  • Check in model rules that a field has a valid enum value:
public function rules()
{
  return array( 
    (...),
    array( 'enum_field', 'in', 'range' => MyEnum::getValidValues() ),
    (...),
  );
}
  • Fill a radio button list:
echo $form->radioButtonList( $model, 'enum_field', MyEnum::getDataForRadioButtonList() );
  • Fill a drop down list:
$form->dropDownList( $model, 'enum_field', MyEnum::getDataForDropDown() );
  • To use an individual enum value, use it as a constant
echo MyEnum::MY_ENUM_VALUE_1;

Support

Support post

Total 3 comments

#3311 report it
twocandles at 2011/04/01 04:25pm
php >= 5.3.0

@Raoul Your absolutely right. I've updated the documentation.

#3309 report it
Raoul at 2011/04/01 04:13pm
php >= 5.3.0

This extension uses the get_called_class function which is only available in php 5.3.0 (or more).

#2812 report it
Anupam at 2011/02/13 02:45pm
My implementations as a LookUp Class

just for the same functionality i have created a lookUp where are declared const values and methods for populating various kinds of Lists & selects from the DB.

Leave a comment

Please to leave your comment.

Create extension
Downloads