Still figuring out how Yii wants me to do things I’d otherwise know how to do if I were rolling my own framework.
I’ve got some database tables that amount to an enumeration set. For example a UserTypes table:
ID Descr Constant
1 Guest GUEST
2 User USER
3 Admin ADMIN
I want to run a script that loops over the values in this and other similar tables to define the items in the Constant column.
foreach($userTypes as $u)
define($u->constant, $u->id);
Then other places I can do something like this:
switch($userTypeId)
{
case ADMIN:
...
}
I’ve used this before on other projects and it is very handy if the database IDs get jumbled as the code doesn’t need to know that ADMIN is 2 or 1 or whatever.
So where do I put this code? I want to run it before every request as I need those constants to be defined before they are reached in the code.
I’d also welcome a “the Yii way of doing these sort of constants is as follows:” if there’s a better way.
I noticed the CEnumerable class. In case you don’t know I’ll put the entire code here:
class CEnumerable
{
}
Really!