Cakephp将值保存到数据库中
问题描述:
我有一个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是什么原因?
答
是否确定不要调用该函数两次或更多?或者,也许数据库引擎增加它(像ON UPDATE增量SQL语句)?
您是否调试过($ newVoteCount)...? – 2012-07-27 11:18:20