Hi all,
I'm after a complete and practical private messaging system which has a thread feature. There are some topics in this forum about pm systems but none of them satisfied me enough and they are only related to the database structure. I want to talk about usage also (SQL queries etc.).
The best database design I can find is from this forum (http://www.phpfreaks.com/forums/index.php/topic,262543.msg1237675.html#msg1237675). It has 2 seperate tables. One for private message itself, and second for its relations to the receiver etc.
1st table:
CREATE TABLE IF NOT EXISTS `pm` (
`pmID` int(11) NOT NULL auto_increment,
`parentID` int(11) NOT NULL default '0',
`senderID` int(11) NOT NULL,
`title` varchar(100) default NULL,
`content` text,
`dateSent` datetime default NULL,
`senderDelete` tinyint(4) NOT NULL,
PRIMARY KEY (`pmID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
2nd table:
CREATE TABLE IF NOT EXISTS `pmInfo` (
`messageID` int(11) NOT NULL auto_increment,
`receiverID` int(11) NOT NULL,
`receiverRead` tinyint(4) NOT NULL,
`dateRead` datetime default NULL,
`receiverDelete` tinyint(4) NOT NULL,
PRIMARY KEY (`messageID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
Are we all clear on this database structure? If not, please suggest.
Lets say that we all agreed on this db structure. Now what about the usage?
As I know, best practice would be minimizing the SQL queries ran.
Here we go
POSTING MESSAGE
There are two types of posting messages. 1- New thread, 2- Reply
When a new message is sent I call 3 SQL queries:
1- INSERT query to pm table
2- INSERT query to pmInfo table
3- UPDATE query to make parentID = pmID
When a new reply is sent I call 2 SQL queries:
1- INSERT query to pm table
2- INSERT query to pmInfo table
In the PHP side, for a new thread there is no problem. User selects receiver (so receiverID), and parentID is set to pmID. But for the reply, I have some problems.
In the reply form I'm using a hidden input which holds the parentID for that thread. But with this usage, how can I get the receiverID? If I use hidden parentID to get receiverID I need one more SELECT query (which is unwanted) and I may not find the correct receiverID since I may send the main thread to multi-users.
Lets have a table example:
USERS TABLE
userID username
1 Joe
2 Paul
3 Nick
PM TABLE
pmID parentID senderID title content
1 1 1 Hi! Whats up? -> pm(1)
2 2 3 Hello Hi there.. -> pm(2)
3 1 2 null Fine, you? -> this is a reply to pm(1)
4 1 1 null Fine -> this is a reply to the reply above
5 1 2 null Thanks dude -> this is a reply to pm(1)
PMINFO TABLE
messageID receiverID
1 3 -> message sent by Joe to Nick
1 2 -> same message sent to Paul also
2 2 -> a message sent by Nick to Paul
3 1 -> reply sent by Nick to Joe
4 3 -> reply sent by Joe to Nick
5 1 -> reply sent by Paul to Joe
When Joe replies to Nick with the pmID 4, how can I get the Nick's userID from the table? If I select with respect to parentID, Joe sent the same message to Paul also, it can be confused.
Since I'm listing a thread using the parentID in a page, I have no info about the next receiverID.
This is the first question, I wish it is clear enough for you Do you have any suggestions for this issue?
And all other suggestions to any piece of this topic are welcomed
Thanks