2012-07-27 176 views
0

我有一個Movies表和一個Rating表。評級表belongsTo電影和電影hasOne評級。我想增加「vote_count」字段。代碼如下。Cakephp將值保存到數據庫中

public function set_ratings($movieId, $value){ 
    $this->Movie->id = $movieId; 
    $rateMovie = $this->Movie->read(); 
    $oldNoOfVotes = $rateMovie['Rating']['number_of_votes']; 
    debug($oldNoOfVotes); 
    $newNoOfVotes = ++$oldNoOfVotes; 
    $newVoteCount = $rateMovie['Rating']['vote_count'] + $value; 
    $newAverageRating = $newVoteCount/$newNoOfVotes; 
    debug($oldNoOfVotes); 
    debug($newNoOfVotes); 
    $this->request->data['Rating']['id'] = $rateMovie['Rating']['id']; 
    $this->request->data['Rating']['number_of_votes'] = $newNoOfVotes; 
    $this->request->data['Rating']['vote_count'] = $newVoteCount; 
    $this->request->data['Rating']['average_rating'] = $newAverageRating; 
    //$this->request->data['Rating']['id'] = $rateMovie['Rating']['id']; 
    debug($newNoOfVotes); 
    $this->Movie->Rating->save($this->request->data); 
    debug($newNoOfVotes); 
} 

我面對的問題是,如果「vote_count」是說15,當我增加它和調試值是16,但它得到保存爲18 DB是什麼原因?

+0

您是否調試過($ newVoteCount)...? – 2012-07-27 11:18:20

回答

0

是否確定不要調用該函數兩次或更多?或者,也許數據庫引擎增加它(像ON UPDATE增量SQL語句)?