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是什麼原因?
您是否調試過($ newVoteCount)...? – 2012-07-27 11:18:20