ppi / website

The PPI Website
http://www.ppi.io
MIT License
23 stars 15 forks source link

[Blog] Related Posts #58

Closed dragoonis closed 12 years ago

dragoonis commented 12 years ago

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.

dragoonis commented 12 years ago

Done by @BenTheDesigner. Merged into master branch.