Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6196831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:42:37+00:00 2026-05-24T03:42:37+00:00

I’m building a rating system where a user can rate something from 1-5 stars.

  • 0

I’m building a rating system where a user can rate something from 1-5 stars.

I was wondering if there’s a way to automatically calculate all of a specific item’s ratings (from the ratings table where model=’x’ and foreign_key=’y’) on afterSave or something similar.

I can do it in the ratings_controller just fine… just thought it might be more ideal to be done automatically in the model. Can anyone point me in the right direction for this?

I would LOVE to hear that there’s some kind of association setting in CakePHP that allows it to do this for you – something like:

//Rating model
var $belongsTo = array(
    'Restaurant' => array(
        'averageValue' => 'rating
    )
);

But – I’m sure that’s asking to much 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:42:39+00:00Added an answer on May 24, 2026 at 3:42 am

    if you want to save the average into a field in items table then afterSave would probably be the best solution right now.

    The only thing cake can automatically do for you is keeping track of how many ratings an item has (counterCache), but not other aggregate functions.

    virtualField may be good, but I have never used that for aggregate functions, so I’m not sure. Besides, if your ratings don’t change often, it would put unnecessary work on the system.

    In Rating model:

    function afterSave($created){
       $avgValue = $this->Query('SELECT AVG(rating) as rating FROM ratings WHERE ratings.restaurant_id = '.$this->restaurant_id);
       $this->Restaurant->updateRatingAverage($this->restaurant_id,$avgValue[0][0]['rating']);
    }
    

    In Restaurant model

    function updateRatingAverage($id,$avg){  
       $this->id = $id;
       $this->field('your_average_field_here',$avg);
    }
    

    you might want to log the $avgValue to see how it’s structured, but I think I got that right.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I know there's a lot of other questions out there that deal with this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.