Reputation: 2303
I am wondering how to design this best, so i do not re-invent the wheel
i have a rating Entity:
/**
* Type of the read of this comment,for example quality
*
* @ORM\Column(type="string")
*/
protected $type;
/** @ORM\Column(type="datetime") */
protected $created;
/** @ORM\Column(type="integer") */
protected $thread;
/**
*
*
* @ORM\ManyToOne(targetEntity="User")
*/
private $user;
/**
* @ORM\Column(type="decimal", scale=2)
*/
protected $value;
where the type, is the type of rating, for example for "file.quality" or "file.story", for shared videos.
now i want to open the details page for the video, and have the total rating showing (as average of all users....also there are ratings, which have nothing to do with users, but coming from other places ....like IMDB (internet movie database) ratings.
i am thinking of adding a CompleteRating
entity, where i save the average rating and update it directly, whenever a user adds a new vote into Rating
Entity. Maybe through a listener class in symfony2.
is this the best design way? how would you handle this best?
Upvotes: 0
Views: 224
Reputation: 690
I believe there a couple of ways you could do it, whether or not there is a best way is really dependant on how often you want your ratings to update, whether or not you want the server to get stressed, etc
1) You could have two columns in your video entity, CompleteRating, LastRatingUpdateTime. In your repository you could have a GetRating function, whenever it's called it checks the last update time, if it's say an hour later compared to the last rating update, run a database query that updates the rating and output the new rating. This of course means users have to wait to see their votes count but it means that server stress is going to be lower.
2) Perhaps you could hate two separate ratings, 1 for outsourced ratings(IMDB, etc) that only gets updated every night via a cronjob and then the individual ratings that get either calculated when a user adds a rating or even just on the fly, databases are very quick at this sort of thing.
My vote would be 2, it really depends how intensive the code you have in place for scraping IMDB ratings is.
Upvotes: 1