Robust
Well-known member
Alright, I'm really confused here. So I'm making a Best Answer/Q&A manager. I've got a few problems, and have tried resolving them for 2 weeks but found no solution. I'd really appreciate some help.
Problem 1: Showing phrase ba_vote or ba_unvote
So, this function almost identically represents the one of XenForo's Like function. The way I currently do it:
If {xen:helper checkVoteStatus, $user} is true, show ba_unvote, else show ba_vote.
checkVoteStatus calls this method:
If it's an array (true), it means a vote already exists, so it shows ba_unvote, and if it returns false then no vote already exists, and it shows vote.
This works all fine, but damn that's a lot of queries. That's an extra query per post. I could always make one query per page, using WHERE thread_id = ? instead and then filter based on post IDs, but even then that's poorly inefficient. What's the best way of showing vote or unvote based on if they have already voted?
Problem 2: Submitting Votes / Unvotes
I previously posted a thread on too many DB queries (https://xenforo.com/community/threads/optimising-code-too-many-db-queries.97853/). I got given the following, and it sounds like a good idea, but I'm not completely sure on how to execute it.
I think it means to have an array with the data I'd be inputting into the database, and serialise it (the serialize() function), then insert it into the DataRegistry perhaps? Not really sure on how to execute this.
Thanks for any help you can give
Problem 1: Showing phrase ba_vote or ba_unvote
So, this function almost identically represents the one of XenForo's Like function. The way I currently do it:
If {xen:helper checkVoteStatus, $user} is true, show ba_unvote, else show ba_vote.
checkVoteStatus calls this method:
PHP:
/**
* Gets a best answer content record for a user that has voted on a piece of content.
*
* @param integer $postId
* @param integer $userId
*
* @return array|false
*/
public function getCurrentBestAnswerStatusOnPost($postId, $userId)
{
return $this->_getDb()->fetchRow('
SELECT *
FROM ba_votes
WHERE post_id = ?
AND vote_user_id = ?
', array($postId, $userId));
}
If it's an array (true), it means a vote already exists, so it shows ba_unvote, and if it returns false then no vote already exists, and it shows vote.
This works all fine, but damn that's a lot of queries. That's an extra query per post. I could always make one query per page, using WHERE thread_id = ? instead and then filter based on post IDs, but even then that's poorly inefficient. What's the best way of showing vote or unvote based on if they have already voted?
Problem 2: Submitting Votes / Unvotes
I previously posted a thread on too many DB queries (https://xenforo.com/community/threads/optimising-code-too-many-db-queries.97853/). I got given the following, and it sounds like a good idea, but I'm not completely sure on how to execute it.
Well depending on how often you need to read the votes, you might want to consider storing the votes in a serialized fashion like how XF stores data about likes on a per post basis. It stores the likes, username of who did the like, etc. in a serialized array on a per post basis so it doesn't need to do any extra queries when displaying a post. Then anytime a vote is added/deleted/changed, you update that serialized array for the post table.
I think it means to have an array with the data I'd be inputting into the database, and serialise it (the serialize() function), then insert it into the DataRegistry perhaps? Not really sure on how to execute this.
Thanks for any help you can give