Sharing technology, ideas, insights!
Call: +91 710 466 0336         Email: hello@sanisoft.com

Blog

The longer Lithium Blog tutorial using MySQL – Part 2

In the previous article, we learned how to setup Lithium and create a simple blog to add/edit/delete posts. Now, we will see how to add comments to the post and display them back on post view page.

Unfortunately, Lithium doesn’t support relationships yet, so we will query the comments table on our own.

Let us start with creating the comments table

Now, create a model for comments so that we can handle the validations and some before save stuff. Create a new file in your app/models and name it Comment.php and add the following code to it.

I have explained most of the above code in Part 1 so won’t repeat it here again. If you have not read the first part, I suggest you first go through it and then come back here.

We will allow the users to add comments for posts on their individual view pages and also display comments from other users on the same page. To do this, open app/views/posts/view.html.php and add the following code below the existing code.

Wait! Don’t try to view the result immediately as we still need to add some code in posts controller. Open posts controller and the following after the existing uses statements (this will be around line 4 if you are following from the previous article)

Now replace the code in view action with the following

You can see that we are fetching the comments for the post separately. This is because, relationships are not yet supported in Lithium. We can expect them to be in place when version 1.0 is released. But till then, we have to either write the associated queries manually or use some existing ORM. Mariano has written an excellent tutorial on using Doctrine with Lithium. Take a look if you are interested in that kind of solution.

With this much code in place, you can go to your posts/view page and notice a comment form at the bottom. We will now add the code to save the comment by user. This will go into the CommentsController. We will also display the validation errors if any. Create a new file in app/controllers and name it CommentsController.php and add the following code to it –

We are simply trying to save the user comment and in case of any validation error, we are fetching the post and its comments and rendering the posts/view template.


Even though there are no view files for comments, you will need to create a comments folder in app/views so that the framework can find the the template using the path given in render().

If you have done everything correctly, then you should be able to add new comments for the given post and see them below the post.

Finally, when a post is deleted, the comments associated with that post should also get deleted. For this, we will add a simple delete filter in Post model. Open app/models/Post.php and the following code in the end of __init() function

We created a comment object and called delete on it and provided the id of post we are deleting. If you are following this tutorial from Part 1, then at this point you have the basic blog with add/edit/delete posts and add/delete comments functionality.

I have updated the Git Repository with the code in this post.

As I have mentioned previously, there are numerous possibilities to extend this code and showcase the power of Lithium. Feel free to fork the Git repository and add more functionality to this base.

About the Author

6 comments

  1. Pingback: The longer Lithium Blog tutorial using MySQL – Part 1 at SANIsoft – PHP for E Biz

  2. Pingback: List of useful Lithium (Li3) resources « Web mellom fjell

  3. Pingback: What are some good tips for using the Lithium Framework (for PHP)? - Quora

  4. Hello Thank you for this great tutorial. i know this is old but please i just need some answers. Do you still use Lithium? what do you say about your experience? any idia how do you handle many-to-many relationships?
    i will appreciate your help a lot.

  5. Thanks a lot for this tutorial. This tutorial’s good, no doubt but kindly write one to code a custom Blogger template in detail. Thanks!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.