0

I have to log every database action on my log table, here is the table:

log_id - INT
user_id - INT
table - VARCHAR(45)
date - INT
action - ENUM('INSERT','DELETE','UPDATE')
new_value - VARCHAR(255)
old_value - VARCHAR(255)

If a user create a new blog post, for instance, I have to save the user id, the table where the object will be inserted, date, action will be "INSERT", new_value will be the php object serialized, the old_value will be empty.

In the case of a update, I need to save the new_value with the new object but before that I need to save the current values, also serialized on the old_value column.

In the case of a delete, I need to save the serialized object on the old_value column and leave the new_value empty.

I was reading about CI Hooks but it seems it doesn't work with database actions, only with controllers and system. Is there a way to use hooks or there is a better solution?

Thanks in advance for any help.

Sorry about the long description, I'm not good with words.

8
  • You can use hooks - if it's a post hook, then I think you'd still have access to the CI object - are you using $ci = get_instance(); in your hook? If it's a pre system hook, then the CI object is not yet available so you'd have to do it the old fashioned way with PDO or something. Commented Mar 6, 2012 at 19:59
  • 1
    Thanks @Stevo, in the case of not a post action, hooks can't handle it right? Commented Mar 6, 2012 at 20:02
  • 1
    not sure what you mean by 'not a post action'. Commented Mar 6, 2012 at 20:04
  • if the user wants to delete something, I'll give him a link that will call a delete function, so, it is not a post action, is it? for post I understand it is something sent via form Commented Mar 6, 2012 at 20:06
  • 1
    let us continue this discussion in chat Commented Mar 6, 2012 at 20:15

1 Answer 1

2

1

You can extend the db driver class so as to intercept all queries to the db. You can grab the CI super object to get user info. You can then modify the class to do the regular db work and also store the queries/whatever else in the db.

codeigniter - pyrocms intercept and modify all queries; extending active record

http://codeigniter.com/wiki/Extending_Database_Drivers

Pros:

-Background operation; happens transparently--no modification necessary on existing constructors

2

Another option (less work upfront, more work going forward) is to create a library and steal a bunch of the profiler [system/library/Profiler.php] class code, and call the library in your controller constructors. In the library, you can modify the stolen profiler code to grab the queries and other data and store to the db (also, CI super object for user data).

Pros:
-Can send other data from controllers if desired (e.g. can send params to the library method when called in controllers constructor)

Cons:
-Gotta change those constructors!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.