views:

67

answers:

3

So I'm working on a framework-esque system with one of my co-workers. Our current challenge is how to best implement statuses. Oftentimes, a status will carry with it unique data (a color for a table row, or text to be displayed to a user. etc). Currently, we have a statuses table which contains all this data. Contained in that table is a column: "css_class", which, whenever a record has that status, the specified CSS class is attached to the element (in this case a tr). Also, in order to assign another record a specific status, a foreign key is specified in that database table (in this case, a user has a specific status. So in the users table, there is a statuses_id foreign key). This implementation works alright, but there are a few problems. First, what if I need to perform a specific action in PHP if a record is in a specific status? The way we do it now is something like this:

if($user->status==0) 
{
    //execute some code
}

This really doesn't work well if statuses can change. Change one status, and the associated code either breaks or behaves differently than intended.

The other issue, and the main reason for posting a question is that the table contains the column "css_class". This is very versatile and allows us change the style of a specific status very quickly. But we really dislike the idea of putting code inside a database. Perhaps having CSS classes in a database isn't necessarily a bad thing, but I really don't know what the common practice is. Any ideas?

EDIT:

What I've gathered from the first few answers is that I should keep all my view stuff out of my model stuff in order to maintain an MVC framework. My argument is that if I keep the css_class name out of the database, then I'm checking the status id in the view in order to decide which class to assign it. So if I put the class in the database, I'm putting View information in the Model. If I don't put CSS classes in the database then I'm putting Model information in the View (checking which ID it belongs to). So by not muddying up the Model, I muddy up the view instead.......

A: 

If you want to continue your paradigm of storing this in the DB, you could make another table that maps VARCHAR names of the statuses to their corresponding INTEGER IDs.

However, if this was my framework. I would not be storing view information like this in the database. This would be handled by the V of my MVC setup.

Ben S
This is exactly what I'm trying to avoid. I would prefer to keep my M, v, and C separate. Currently I'm putting my V in my M, and Rob recommends putting some of the M in the V (If I have a class named for an ENUM value...) Is there a happy medium?
SpaDusA
A: 

From a data modelling point of view:

  • Have a different table for each "kind" of status; keep user statuses separate from page statuses (for example) - group the like entities together.
  • Don't put the CSS classes into the database, but use some form of status indicator - this could be an ENUM column, if you know the set of possible statuses up front. Transform this into the appropriate CSS class in the view layer. You don't want to end up in a situation where your CSS can't be changed because some data in the database prevents it.
Rob
In most cases, I would know the statuses ahead of time and the ENUM wouldn't be a bad idea. However, we have had a non-negligible number of clients that require the ability to manage their statuses, in which case the ENUM idea would break...
SpaDusA
+2  A: 

The most elegant way I've seen this solved so far (and I've worked with a few MVC implementations now) is to store only the relevant data in the database. E.g. you'd store status="red" in the database, and leave it up to the view to know what to do with a red status, in terms of CSS. The problem is then solved by designing a sufficiently advanced View layer that creates reusable structures -- that way you don't need to always be updating things on a page-by-page basis when the css changes.

Passing this information up to the Model somewhat defeats the point of the content/presentation separation, because now your code needs to know to pull presentation information off the database and forward it along to the View level or, shudder, you'll be pulling that stuff from the database right in your View layer code, which makes maintenance a nightmare, as you've now lost control over the information flow.

Gus Melo
The thing I really dislike about this idea is that we'd have code like if(status==0) ... which is very difficult to maintain if status 0 decides to change
SpaDusA
If you can post a more concrete example of the situation you're facing, you might be able to open this up to more responses. I'm having a hard time picturing the situation you're describing being a problem, but I'm sure it's a communication issue.
Gus Melo
Sure thing. I'll work on trying to get an example together. Thanks!
SpaDusA