I need a table to log certain actions users make in WordPress.
As of now, this is the database schema I have camp up with:
id bigint(20) NOT NULL AUTO_INCREMENT,
uid bigint(20) NOT NULL,
type VARCHAR(256) NOT NULL,
data1 TEXT NOT NULL,
data2 TEXT NOT NULL,
data3 TEXT NOT NULL,
timestamp bigint(20) NOT NULL,
UNIQUE KEY id (id)
Let me clarify:
uid: User ID of the wordpress user
type: Type of action the user made (can be 'comment', 'new_post', 'login', etc)
data1/2/3: additional data (for example, ID of comment or post made)
To display the logs, I would query the database and run through a certain filter to get the text to display for that particular log. So it works something like this:
if( $type == 'comment') {
$comment = get_comment( $data1 );
$user = get_user($uid);
echo "User {$user->name} has made a <a href='{$comment->permalink}'>comment</a>";
}
Is this the most efficient way of doing things? It seems quite fine to me as I do not want to just store HTML in the logs table to be outputted.
However, the problem comes where I want to hide a particular log entry when certain conditions are met. Like, for example, if a comment no longer exists, I want to hide that entry. This would pose some problems with pagination. Any suggestions on how I can overcome this?
Thanks!
EDIT:
myplugin_transactions
id bigint(20) NOT NULL AUTO_INCREMENT,
user_id bigint(20) NOT NULL,
type VARCHAR(256) NOT NULL,
timestamp bigint(20) NOT NULL,
UNIQUE KEY id (id)
myplugin_meta
id bigint(20) NOT NULL AUTO_INCREMENT,
txn_id bigint(20) NOT NULL,
key VARCHAR(256) NOT NULL,
data TEXT NOT NULL,
UNIQUE KEY id (id)
Lets say I want to select * from myplugin_transactions where data1 would usually have had been 'x' and data2 been 'y'. How should I do it in this case?
SELECT * FROM myplugin_transactions LEFT JOIN myplugin_meta ON myplugin_transactions.id = myplugin_meta.txn_id WHERE ( ... ? )
BIGINTforuser_id? Do you expect to have more than 4 billion users? Thetimestampcolumn should probably be theTIMESTAMPtype. Try to avoid theTEXTtype; it will force any sorting to be done on disk. Unless you really need more than 255 characters, useVARCHAR(255). Thetypecolumn on the main table should probably beVARCHAR(255)since making it 256 adds an extra byte. These changes can significantly reduce the size of the data and indexes.BIGINTif you're being consistent. What will the timestamp value actually be? Will it be a unix timestamp or a formatted date? Also, will it always be when the row is inserted to the table or will it be some other time? You should also make youINTtype fieldsUNSIGNED; usingBIGINT, you probably won't notice any difference, but it's the right thing to do.INTtypeUNSIGNEDmakes it so it can only have positive values, effectively doubling the max value.TIMESTAMPwould probably be the best type. If you really need to manipulate the time with timezones, you can convert it.TIMESTAMPis smaller and basically is a unix timestamp to the server.TIMESTAMPalso has the benefit of being able to use the current time as the default value, allowing you to simplify the queries by omitting the timestamp and having the server use the current time.