MySQL:更新具有相同值的字段


MySQL: Updating a Field With The Same Value

我使用以下方法来更新comment表中的字段comment_confidence

public function update_comment_confidence($confidence_score)
{
    $this->db->update($this->_table, array('comment_confidence' => $confidence_score), array('comment_id' => self::$comment_id));
    if($this->db->affected_rows() < 1) throw new Exception('failed to update comment     confidence');
    return;
}

下面是调用上述方法的代码:

$this->db->trans_start();
$this->create_vote($vote);
try
{
    $total_votes = $this->read_comment_total_votes();
    $confidence_score = $this->ranking->confidence($total_votes['upvote'],$total_votes['downvote']);
    // SKIP UPDATING COMMENT CONFIDENCE IF ITS CONFIDENCE IS 0 AND THE CONFIDENCE_SCORE IS 0
    $this->article_comment_model->update_comment_confidence($confidence_score);
}
catch(Exception $e)
{
    // transaction is rolled back
    throw new Exception($e);
}
$this->db->trans_complete();

当 update_comment_confidence() 方法传递的值为 0 并且数据库中的值已经为 0 时,将引发异常。并且所有表都将回滚。这是因为更新期间没有受影响的行。这不是我想要的功能。

当字段包含相同的值时,我该怎么做才能防止在update_comment_confidence中引发异常?

好吧,

如果我是你,我不会在受影响的行<1 时抛出异常

if($this->db->affected_rows() < 1)

出现错误时引发异常。