Cake Star Rating - The Code

AJAX star rating using CakePHP

This is an intermediate article and as such should not be attempted unless you have a good understanding of using Cake, including its AJAX support.

The complete code including supporting css, javascript, controller, models and views can be downloaded below.

As mentioned in my first article, the database backend consists of two main tables -- one to hold user information and one to hold article information including ratings. There's also an intersection table to map the relationship between the two.

From there, you need to include the component and helper in your controller, then you'll want two basic actions -- one that includes a view for the initial display of the article and its rating and one to process a new vote.

class RatingsController extends AppController
    var $name = 'Ratings';
    var $components = array('RfRating');
    var $helpers = array('RfRating');

    /* you'll also want to add a User model here, but for
     * testing purposes, we don't need to have it

    var $uses = array('Article', 'Vote');

    function index() {

        /* getRatingInfo($articleId, $userId) - typically
         * pull these params from url/session/post data/etc.

                    $this->RfRating->getRatingInfo(1, 2));


     * record a new rating
     * calls component vote method
     * @param int rating value
     * @param int article id

    function vote($vote, $articleId) {


        /* can pull the user id from session or
         * set a hidden field etc.

        $userId = 2; // for testing

                    $this->RfRating->vote($vote, $articleId, $userId));

        /* set showText if you want to show a text
         * representation of the rating

        //$this->set('showText', '1');



The view file will use the helper to display the rating stars. Specific rating information is passed from the component through the controller to the view that calls the helper to write the code which uses the css to show the stars to the browser. If you followed all of that, congratulations! If not, it all means to use the following in your view:

echo $rfRating->ratingBar($ratingInfo);

Using the rating information passed to it from the controller, it displays the rating stars and if a user has not yet voted, writes the AJAX needed to call the vote action. The vote action will call the component to process the vote and return the vote view back to the rating div to be dynamically updated.

Depending on how many people (if any) use this, I'll update this to go a little more in-depth with specific usage and options, but rest assured the code is commented to allow you to discern how you can use it. I would like to note one thing however, to hide the number of votes, set display:hidden in the css for the #totalvotes style. In the mean time, if you have any questions or comments, please contact me.


The following zip file contains all the component and helper as well as all necessary supplementary files such as the models, controller, views, css and javascript so you should be able to drop everything in and test it out.