Code Design - Models needing approval

Hi all,

I have a web app that allows users to request changes to be made that will not take place until an admin user has manually reviewed the change and approved it.

So the question is how do I store all the changes that are awaiting approval without actually making the changes to the database? I could add an row to each of the tables that can have changes requested which could contain status of the model (similar to posts waiting for approval to be posted in a blog), but the changes can get quite complicated.

The challenge is that there are five different models that can have changes requested and each model has one to four different ways that the model can be modified, all of which are very different from each other.

Each model would have to have a significant amount of code written to allow all the different types of changes take place. And I was hoping to avoid writing all of that, very specific code.

For example, there is a many-to-many relationship between users and roles. A user can request that his contact information and roles be changed simultaneously. This would require a new User object to be created with the updated contact information while the old user object would still have to be stored in the database until the new one was approved or denied (which would cause conflicting keys).

At the same time a new user_role object would have to be created in the database since the user wants to add a new role.

This is just a sample of the type of issues that I have to deal with. I am just trying to come up with a clean way to simply manage all of the changes that are pending approval.

The developer before me wasn’t using Yii and created a table in his database that contained code to be executed for each requested change. When the change was approved, the code was evaluated which updated the database.

I thought of doing something similar in Yii, but it is not very easy to do and the whole idea of storing code in a database feels kind of hacky.

I apologize if my description of the problem is not clear, but does anyone have any suggestions for designing a system to allow users to request changes that pend approval from admins?

I’m going to assume you already considered a simple status flag system and rejected as too simple.

Maybe create a table like this:

id

model

modelId

attributes(enum? array?)

oldValue

newValue

requestDate

approveDate

status (pending, approved etc)

This way it is universal. You can probably create a behavior to encapsulate this code and attach it to any model. You will have to tweak this structure for m:m relationships but this works for a single attribute of a model as a starting point.

This was pretty close to the solution that I have playing around with. My biggest problem is that I have not found a way to handle many-to-many relationships as you mentioned. If I can’t come up with anything else, I will just hard-code a function to deal with each individual many-to-many relationship. I only have a few.

I have a few situations such as this in my apps. I really like MVC and the Yii way of doing things, right up until it gets too clever and in my way. I’m not above going outside of this approach if it means I get things done.