Model Cakephp将值保存到数据库中
我有一张电影表和一张评级表。电影的分级表,电影只有一个分级。我想增加“投票计数”字段。代码如下Model Cakephp将值保存到数据库中,model,cakephp-2.1,Model,Cakephp 2.1,我有一张电影表和一张评级表。电影的分级表,电影只有一个分级。我想增加“投票计数”字段。代码如下 public function set_ratings($movieId, $value){ $this->Movie->id = $movieId; $rateMovie = $this->Movie->read(); $oldNoOfVotes = $rateMovie['Rating']['number_of_votes']; debug(
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);
}
我面临的问题是,如果“投票计数”是15,当我增加它并调试时,值是16,但在DB中,它被保存为18。原因是什么 您确定没有调用该函数两次或两次以上吗?或者数据库引擎会增加它的增量(类似于更新增量SQL语句)您调试过($newVoteCount)?