Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
How can I update another model from current model?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
djo26  
View profile  
 More options Nov 4, 11:31 am
From: djo26 <djo2...@gmail.com>
Date: Tue, 3 Nov 2009 16:31:56 -0800 (PST)
Local: Wed, Nov 4 2009 11:31 am
Subject: How can I update another model from current model?
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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert P  
View profile  
 More options Nov 4, 10:05 pm
From: Robert P <shiftyrobs...@gmail.com>
Date: Wed, 4 Nov 2009 03:05:31 -0800 (PST)
Local: Wed, Nov 4 2009 10:05 pm
Subject: Re: How can I update another model from current model?
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 <djo2...@gmail.com> wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
djo26  
View profile  
 More options Nov 4, 10:41 pm
From: djo26 <djo2...@gmail.com>
Date: Wed, 4 Nov 2009 03:41:49 -0800 (PST)
Local: Wed, Nov 4 2009 10:41 pm
Subject: Re: How can I update another model from current model?
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 <shiftyrobs...@gmail.com> wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
djo26  
View profile  
 More options Nov 7, 1:03 pm
From: djo26 <djo2...@gmail.com>
Date: Fri, 6 Nov 2009 18:03:36 -0800 (PST)
Local: Sat, Nov 7 2009 1:03 pm
Subject: Re: How can I update another model from current model?
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 <djo2...@gmail.com> wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
MeanStudios  
View profile  
 More options Nov 7, 1:29 pm
From: MeanStudios <cody.lundqu...@gmail.com>
Date: Fri, 6 Nov 2009 18:29:39 -0800 (PST)
Local: Sat, Nov 7 2009 1:29 pm
Subject: Re: How can I update another model from current model?
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 <djo2...@gmail.com> wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google