Yii Framework Forum: [EXTENSION] enum - Yii Framework Forum

Jump to content

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

[EXTENSION] enum Enums for PHP in Yii Rate Topic: -----

#1 User is offline   twocandles 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 58
  • Joined: 23-December 10

Posted 07 February 2011 - 05:10 AM

I've published a piece of code to try to emulate enums in PHP.

You can take a look at it here:

http://www.yiiframew...extension/enum/

It contains methods to make use of yii components (dropdowns, radio button lists, model rules, etc.) easier.

Some examples:

Declaring

class MyEnum extends Enum
{
  const MY_ENUM_VALUE1 = "MY_VALUE_1";
  const MY_ENUM_VALUE2 = "MY_VALUE_2";
}


Translating (i18n support in Yii)

return array(
    MyEnum::MY_ENUM_VALUE1 => 'My value 1',
    MyEnum::MY_ENUM_VALUE2 => 'My value 2',);


Rules validation

public function rules()
{
  return array( 
    (...),
    array( 'enum_field', 'in', 'range' => MyEnum::getValidValues() ),
    (...),
  );
}


Radio button:

$form->radioButtonList( $model, 'enum_field', MyEnum::getDataForRadioButtonList() );


Drop down list:

$form->dropDownList( $model, 'enum_field', MyEnum::getDataForDropDown() );


Use an enum value:

echo MyEnum::MY_ENUM_VALUE_1;


Declare a DBEnum

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";
  }*/
 
}


A DBEnum is like an Enum but it performs a first-time check of the declared values agains a DB table. If not interested in this feature, just use the regular Enum class.

This post has been edited by twocandles: 21 April 2011 - 11:41 AM

0

#2 User is offline   twocandles 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 58
  • Joined: 23-December 10

Posted 13 February 2011 - 11:58 AM

Updated with a better support for DB Enums. Also fixed a little bug with dropdowns...
0

#3 User is offline   snapCrackerJoe 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 19-December 10
  • Location:United States

Posted 23 February 2011 - 06:21 AM

Hi,

When I try to use DBEnum I get this error:
Missing argument 1 for CDbConnection::createCommand(), called in /var/www/w1/protected/extensions/DBEnum/DBEnum.php on line 67

In my Controller I have:
class EventTypeEnum extends DBEnum
{
const MY_ENUM_VALUE1 = "Rye";
const MY_ENUM_VALUE2 = "Pumernickel";

protected function getDBField()
{
return 'EventType';
}
protected function getDBTable()
{
return 'Event';
}
}

In my View I Have:

<?php
echo CHtml::activeDropDownList( $eModel, 'EventType',
EventTypeEnum::getEnum()->getDataForDropDown());
?>


Do you have any idea what Im doing wrong?

Thanks
0

#4 User is offline   twocandles 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 58
  • Joined: 23-December 10

Posted 26 February 2011 - 12:11 PM

Hi, sorry for the delay.

Have you tried debugging to see what's happening?

Could you try this, please?

<?php
$myEnums = EventTypeEnum::getEnum()->getDataForDropDown();
/* ***** dump $myEnums to check its value **** */
echo CHtml::activeDropDownList( $eModel, 'EventType',
EventTypeEnum::getEnum()->getDataForDropDown());
?>


Have you added "Rye" and "Pumernickel" to your "Event" table ?

BTW, just an advice: your constants should look like:

class EventTypeEnum extends DBEnum
{
const RYE = "Rye";
const PUMERNICKEL = "Pumernickel";
(...)
}


so when you use them in your code it's easier to identify them ;)
0

#5 User is offline   twocandles 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 58
  • Joined: 23-December 10

Posted 21 April 2011 - 01:56 AM

Updated with a cleaner interface. Take a look at the extension page
0

#6 User is offline   stetze 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 03-February 13

Posted 01 April 2013 - 06:50 AM

Hi. Thanks for the extension, it works great so far.

I have a question concerning what is stored in the db and what is shown to the user. I want to have Enum of different statuses like

class BookingStatusEnum extends Enum
{
	const requested 	= '10_requested';
	const reserved 		= '20_reserved';
	const booked		= '30_booked';
}


These values are stored in my DB after saving the model/form. But what I want to show to the user is a localized description of these statuses.

Therefore I have a en/enums.php
return array(
	BookingStatusEnum::requested => 'requested',
)


and a de/enums.php:
return array(
	BookingStatusEnum::requested => 'angereist',
)


And the problem is that this localization only works for the language which is not defined as source language. How can I change that?

Thanks in advance,
Stefan
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