模型类增加force方法设置是否强制更新所有数据

This commit is contained in:
thinkphp
2017-11-21 16:46:09 +08:00
parent 44c1257326
commit ea36fdfbe9

View File

@@ -94,6 +94,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
protected $type = []; protected $type = [];
// 是否为更新数据 // 是否为更新数据
protected $isUpdate = false; protected $isUpdate = false;
// 是否强制更新所有数据
protected $force = false;
// 更新条件 // 更新条件
protected $updateWhere; protected $updateWhere;
// 验证失败是否抛出异常 // 验证失败是否抛出异常
@@ -350,6 +352,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return $this; return $this;
} }
/**
* 更新是否强制写入数据 而不做比较
* @access public
* @param bool $force
* @return $this
*/
public function force($force = true)
{
$this->force = $force;
return $this;
}
/** /**
* 修改器 设置数据对象值 * 修改器 设置数据对象值
* @access public * @access public
@@ -1186,12 +1200,16 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
*/ */
public function getChangedData() public function getChangedData()
{ {
$data = array_udiff_assoc($this->data, $this->origin, function ($a, $b) { if ($this->force) {
if ((empty($a) || empty($b)) && $a !== $b) { $data = $this->data;
return 1; } else {
} $data = array_udiff_assoc($this->data, $this->origin, function ($a, $b) {
return is_object($a) || $a != $b ? 1 : 0; if ((empty($a) || empty($b)) && $a !== $b) {
}); return 1;
}
return is_object($a) || $a != $b ? 1 : 0;
});
}
if (!empty($this->readonly)) { if (!empty($this->readonly)) {
// 只读字段不允许更新 // 只读字段不允许更新