Search code examples
javascriptdatabasesymfonyratingrate

Rating System with Stars. Save rate in DB


I created a sort of blog, and I have, for example, articles. In every article I implemented a star rating plugin that works graphically.

This is the screenshot of the view:

enter image description here

To make it works i used the jRate plugin, and i added in the page this JavaScript block:

    {% block javascripts %}
    <script type="text/javascript">
        $(function () {
            var that = this;
            var toolitup = $("#jRate").jRate({
                minSelected: 0,
                                maxSelected: 5,
                                readOnly: false,
                                shape: 'STAR',
                                width: 30,
                                height: 30,
                                precision: 1,

                onChange: function(rating) {
                    $('#demo-onchange-value').text("Il tuo voto: "+rating);
                },
            });

});

    </script>
        {% endblock %}

The "+rating" variable is the rate of course. So i was thinking that I can use that variable to save it in a field of my article table in the db. So I added a new field in the article entity that is:

/**
 * @var integer
 *
 * @ORM\Column(name="rate", type="integer",nullable=true)
 */
private $rate;

How can I save the rating in that field?


Solution

  • For star rating I have been using https://github.com/blackknight467/StarRatingBundle. If you wish to use your own implementation, at least you can look how they do it. Basically the rating field is represented by a hidden input, and when the stars selected changed , also change the value of the input. On form submition the field is saved normally as an integer. I hope this help you.