0

映画テーブルと評価テーブルがあります。評価テーブルの所属映画と映画には 1 つの評価があります。「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になるが、DBでは18として保存されるということです.理由は何ですか?

4

1 に答える 1

0

関数を 2 回以上呼び出していませんか? それとも、データベース エンジンがそれをインクリメントするのでしょうか (ON UPDATE インクリメント SQL ステートメントのようなもの)?

于 2012-07-27T11:35:25.157 に答える