我有一个电影表和一个评级表。Ratings 表属于 Movies 和 Movies hasOne Rating。我想增加“vote_count”字段。代码如下。
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。原因是什么?