Greetings djo26,
If you have access to the vote_id (which I'm assuming you do since it
should be stored in the posts table for each post) you just need to do
something like this:
$this->Post->Vote->id = $this->data['Post']['vote_id'];
That sets up the vote model so you can get that particular record from
the votes table, make your calculations and then re-save your data.
Then do something like
$voteData = $this->Post->Vote->read();
Do whatever with $voteData.
$this->Post->Vote->save($voteData);
Since you specified the Vote ID earlier, this will update the record
instead of make a new one.
I hope this is what you were looking for :).
Best Regards,
cody
On Nov 7, 1:03 pm, djo26 <
[hidden email]> wrote:
> I ended up just creating a stored procedure and call it from my
> controller like:
> $this->Vote->query('CALL my_stored_proc_name('.$input_post_id.')');
>
> I would rather do this in the model but I have no idea how to.
>
> dan
>
> On Nov 4, 12:41 pm, djo26 <
[hidden email]> wrote:
>
> > yes I know how to perform the calculation, and yes I am storing it in
> > my Post model. I do need a Vote model because I'm storing other
> > information as well for the Vote. I did not mention this originally
> > because I was trying to keep my original post simple. What I'm trying
> > to figure out is how to update the Post model with the updated average
> > from a submit of the votes/add controller. I have the Vote model
> > configured with a "belongsTo" relationship to the Post model but I'm
> > not sure how to actually update the Post with the new value.
>
> > thanks, dan
>
> > On Nov 4, 12:05 pm, Robert P <
[hidden email]> wrote:
>
> > > Perhaps the most efficient way of doing this is by storing the total
> > > in your Posts model, which might even remove the need of a Votes
> > > model. I'm presuming that when you say Votes have a percentage you
> > > mean that each Votes score is on a range from 0 to 100.
>
> > > If you add the columns "average_vote" (float) and "total_votes" (int)
> > > to your Posts table then you can recalculate the average every vote
> > > with the following equation:
> > > new average = ((average vote * total votes) + new vote) / (total votes
> > > + 1)
> > > and of course increment the total_votes column.
>
> > > So the pseudocode for Post::addVote(score) is
> > > if post exists
> > > find average_vote and total_votes
> > > set average_vote to ((average_vote * total_votes) + score) /
> > > (total_votes + 1)
> > > set total_votes to total_votes + 1
>
> > > On Nov 4, 8:31 am, djo26 <
[hidden email]> wrote:
>
> > > > Hi everyone, I have Posts that have Votes, and the Vote has a
> > > > percentage. Every time a new Vote is added I want to calculate the
> > > > average percentage of all Votes of that particular Post and update the
> > > > Post average percentage. Besides a custom query I have no idea how to
> > > > do this in my model. Anyone else on here know? I'm relatively new to
> > > > this so it might be simple, but I'm not sure how to do it right now.
>
> > > > Any help is appreciated.
> > > > thanks, dan
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to
[hidden email]
To unsubscribe from this group, send email to
[hidden email]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en-~----------~----~----~----~------~----~------~--~---