News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Lee Davis' Blog:
The enum conundrum
July 06, 2012 @ 11:56:52

In a new post to his blog Lee Davis describes the enum conundrum - what's the right solution for effectively using ENUM-type fields in your data?

So a user signs up and I want to store a status that reflects their account, or at least an identifier representing that status. Their account could be active, disabled (temporarily), pending approval or maybe deleted. Should I use an enum? I've heard they're evil. Maybe having a reference table with statuses would be better? But now I have to manage a separate table just for that one snippet of data, is that overkill? Could I maybe use that status table for other entities? Or, could I instead just use an integer and reference it on the code level? What is the right solution?

He presents three of the most common situations he's seen for people using enums in the application:

  • "I used enums all over the place" (maintenance between code and DB values)
  • "use a reference table"
  • "I could use a class constant to represent the enum" (enforced in the app)

Of the three, he suggests the third as the option with the most advantages. Not only does it make it simpler to get the allowed values for the field, but you're also more flexible in the kinds of validation you can do on the values.

0 comments voice your opinion now!
enum conundrum reference table constant maintenance


blog comments powered by Disqus

Similar Posts

The Shadow Fox Network: Creating A Scaffold-like Class in PHP or An Automatic CMS For a Table

php|architect: Managing Layout in PHP-GTK 2

SitePoint PHP Blog: Which reference sites do you trust?

Freek Lijten's Blog: The real problem of the hash table dos attack

ProDevTips.com: File Uploads with PHP Doctrine


Community Events





Don't see your event here?
Let us know!


refactor series threedevsandamaybe install language unittest interview framework release code laravel developer symfony2 configure opinion list podcast testing community introduction

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework