0

我有一个电影表和一个评级表。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。原因是什么?

4

1 回答 1

0

您确定不调用该函数两次或更多次吗?或者数据库引擎可能会增加它(类似于 ON UPDATE 增加 SQL 语句)?

于 2012-07-27T11:35:25.157 回答