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

Chris Shiflett's Blog: PHP Quick Reference

PHP-Coding-Practices.com: How Table Migrations In CakePHP 1.2 Can Save Your Life

NetTuts.com: Refactoring Legacy Code: Part 2 - Magic Strings & Constants

Zend Developer Zone: Creating Data Tables With PEAR Structures_DataGrid

Lee Davis' Blog: The enum conundrum


Community Events





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


deployment series symfony threedevsandamaybe conference bugfix code zendserver introduction interview framework release list laravel api tips voicesoftheelephpant language podcast community

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