Welcome Guest, Not a member yet? Register   Sign In
What's the best way to create 'hooks' between the database and application?
#1

[eluser]charlieD[/eluser]
For example, you have 3 user types:

User type table:
Code:
id | name
===================
1  | Visitor
2  | Paying member
3  | Administrator

You have a piece of code that runs only for the admin user type.

You don't want to reference the ID or name directly in the code. (IDs don't make the code easy to read and names can change for editorial reasons).

What's the best approach for creating a hook to the data so you can check the user type in an ifblock for example?

1. Do you create a config file and use constants that refer to the ID, e.g.:
Code:
<?php
define('USER_VISITOR', 1);
define('USER_MEMBER', 2);
define('USER_ADMIN', 3);

if ($user->getTypeId() == USER_ADMIN) {
    //Do stuff
}

2. Or, do you add something like a 'slug' column to the database, that doesn't change:
Code:
id | name          | slug
=============================
1  | Visitor       | visitor
2  | Paying member | member
3  | Administrator | admin

And then access it in the code like this:
Code:
<?php
if ($user->getTypeSlug() == 'admin') {
    //Do stuff
}

The first method seems more programmatically correct and is probably slightly faster as it works directly with IDs, but the second method prevents you from having to define loads of constants and keeps everything in the database. Plus it can be used for prettier URLs because you're using a word instead of a number, which could be beneficial in some situations.

Or are there other much better ways of doing this (without enums)?


Messages In This Thread
What's the best way to create 'hooks' between the database and application? - by El Forum - 07-22-2009, 03:48 PM



Theme © iAndrew 2016 - Forum software by © MyBB