Add "related posts" on the right side of a post page.
Ajax to our route
This means when we perform our AJAX call we should be providing the postID
AJAX to: blog/get_related_posts/2.
The route for this should be:
BlogGetRelatedPosts:
pattern: /blog/get_related_posts/{postID}
defaults: { _controller: "Application:Blog:getRelatedPosts"}
requirements:
_method: POST
The controller
The controller method getRelatedPosts should be something like this:
public function getRelatedPostsAction()
{
$bs = $this->getBlogPostStorage();
$response = array('posts' => array());
foreach($bs->getRelatedPostsByTag($post) as $relatedPost) {
$response['posts'][] = array(
'id' => $relatedPost->getID(),
'title' => $relatedPost->getTitle()
);
}
return json_encode($response);
}
The Storage code
The getRelatedPostsByTag should be
public function getRelatedPostsByTag($post)
{
$tagIDs = array();
foreach($post->getTags() as $tag) {
$tagIDs[] = $tag->getID();
}
// Some Doctrine DBAL QueryBuilder code for providing in() clauses: http://stackoverflow.com/a/6638146
// SELECT post_id from blog_post_tags WHERE tag_id IN($tagIDs)
}
The mustache template rendering
Look at blog.js to see how they work already work for blog/get_recent_comments, it's a copy/paste job really at this point.
Add "related posts" on the right side of a post page.
Ajax to our route
This means when we perform our AJAX call we should be providing the postID
AJAX to:
blog/get_related_posts/2
.The route for this should be:
The controller
The controller method
getRelatedPosts
should be something like this:The Storage code
The
getRelatedPostsByTag
should beThe mustache template rendering
Look at
blog.js
to see how they work already work forblog/get_recent_comments
, it's a copy/paste job really at this point.