Mysql日志获得更新全字段_mysql – 您是否只更新已更改的字段或所有字段?

我认为值得改变 – 但在插入之前可能不值得做一个选择.

我只更新已更改的字段,它是我的DbEntity类的操作的一部分,它遵循activerecord模式.这样做的成本很低,因为我保存了当前记录和原始记录 – 只要加载记录就会复制.

原因很简洁 – 不是真正的表现.您还可以通过在更新字段的旧值上添加where子句来检查并发修改,并抛出相应的错误.

在写/更新方法中:

$s1 = "";

foreach ($this->record as $key => $value)

{

// only update fields that have been changed

if ($value != $this->orig_record[$key])

{

$s1 .= $comma."`$key`='".mysql_real_escape_string($value)."'";

$comma = ", ";

}

}

$query = "UPDATE ".$this->table." SET $s1 where {$this->id_field}='".$this->get_keyfield()."'";

$query .= $this->extra_sql_update;

mysql_query($query);

$ar = mysql_affected_rows();

//

// the number of affected rows is actually those changed by the update operation, which will

// either be zero, or 1. If the query affects more than one row then we have a problem.

if ($ar < 0 || $ar > 1)

{

cbf_error("cbf_dbentity: {$this->table} :: only one row (not $ar) must be affected by an insert operation. $query",

E_USER_ERROR);

}

else

{

$new_id = $this->get_keyfield();

GlobalEventBus::notify_all(new AuditLogSQL($this->table, "update", $query));

}

$this->orig_record = Array();

foreach ($this->record as $key => $value)

$this->orig_record[$key] = $value;

//

// sanity check - ensure that what we have just written is actually there.

$this->load($new_id);

foreach ($this->orig_record as $key => $value)

if (trim($this->record[$key]) != trim($value)

&& (!$this->record[$key] == "0" && $value=""))

cbf_error("cbf_dbentity: {$this->table} :: record differs during write after reload: field $key was \"$value\", after write it is now \"".

$this->record[$key]."\"",E_USER_ERROR);

在加载方法中

$this->orig_record = Array();

foreach ($this->record as $key => $value)

$this->orig_record[$key] = $value;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值