改进Model类

This commit is contained in:
thinkphp
2016-04-08 12:47:09 +08:00
parent 1e4ec8d3d6
commit a4cc8c2ca1

View File

@@ -61,6 +61,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
protected $type = []; protected $type = [];
// 当前执行的关联类型 // 当前执行的关联类型
private $relation; private $relation;
// 是否为更新数据
protected $isUpdate;
/** /**
* 架构函数 * 架构函数
@@ -288,12 +290,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
} }
/** /**
* 保存当前数据对象的值 * 保存当前数据对象
* @access public * @access public
* @param array $data 数据 * @param array $data 数据
* @param array $where 更新条件
* @return integer * @return integer
*/ */
public function save($data = []) public function save($data = [], $where = [])
{ {
if (!empty($data)) { if (!empty($data)) {
foreach ($data as $key => $value) { foreach ($data as $key => $value) {
@@ -311,36 +314,54 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
} }
// 数据自动完成 // 数据自动完成
foreach ($this->auto as $field) { $this->autoDataComplete($this->auto);
if (!in_array($field, $this->change)) {
// 没有经过修改器赋值则进行自动完成 if ($this->isUpdate()) {
$this->__set($field, isset($this->data[$field]) ? $this->data[$field] : null); // 更新数据
if (false === $this->trigger('before_update', $this)) {
return false;
} }
}
if (false === $this->trigger('before_insert', $this)) { // 自动更新
return false; $this->autoDataComplete($this->update);
}
// 自动写入 // 去除没有更新的字段
foreach ($this->insert as $field) { foreach ($this->data as $key => $val) {
if (!in_array($field, $this->change)) { if (!in_array($key, $this->change) && !$this->isPk($key) && !isset($this->relation[$key])) {
$this->__set($field, isset($this->data[$field]) ? $this->data[$field] : null); unset($this->data[$key]);
}
} }
}
$result = self::db()->insert($this->data); $db = self::db();
if (!empty($where)) {
// 获取自动增长主键 $db->where($where);
if ($result) {
$insertId = self::db()->getLastInsID();
if (is_string($this->pk) && $insertId) {
$this->data[$this->pk] = $insertId;
} }
} $result = $db->update($this->data);
// 新回调 // 新回调
$this->trigger('after_insert', $this); $this->trigger('after_update', $this);
} else {
// 新增数据
if (false === $this->trigger('before_insert', $this)) {
return false;
}
// 自动写入
$this->autoDataComplete($this->insert);
$result = self::db()->insert($this->data);
// 获取自动增长主键
if ($result) {
$insertId = self::db()->getLastInsID();
if (is_string($this->pk) && $insertId) {
$this->data[$this->pk] = $insertId;
}
}
// 新增回调
$this->trigger('after_insert', $this);
}
// 写入回调 // 写入回调
$this->trigger('after_write', $this); $this->trigger('after_write', $this);
@@ -349,68 +370,51 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
} }
/** /**
* 更新当前数据对象 * 是否为更新数据
* @access public * @access public
* @param array $data 数据 * @param bool|null $update
* @param array $where 更新条件 * @return void
* @return integer
*/ */
public function update($data = [], $where = []) protected function isUpdate($update = null)
{ {
if (!empty($data)) { if (is_bool($update)) {
foreach ($data as $key => $value) { $this->isUpdate = $update;
$this->__set($key, $value); return $this;
}
if (isset($this->isUpdate)) {
return $this->isUpdate;
}
// 根据主键判断是否更新
$data = $this->data;
$pk = $this->pk;
if (is_string($pk) && isset($data[$pk])) {
return true;
} elseif (is_array($pk)) {
foreach ($pk as $field) {
if (isset($data[$field])) {
return true;
}
} }
} }
// TODO 完善没有主键或者其他的情况
return false;
}
// 数据自动验证 /**
if (!$this->dataValidate($this->data)) { * 数据自动完成
return false; * @access public
} * @param array $auto 要自动更新的字段列表
* @return void
if (false === $this->trigger('before_write', $this)) { */
return false; protected function autoDataComplete($auto = [])
} {
foreach ($auto as $field) {
// 数据自动完成
foreach ($this->auto as $field) {
if (!in_array($field, $this->change)) {
// 没有经过修改器赋值则进行自动完成
$this->__set($field, isset($this->data[$field]) ? $this->data[$field] : null);
}
}
if (false === $this->trigger('before_update', $this)) {
return false;
}
// 自动更新
foreach ($this->update as $field) {
if (!in_array($field, $this->change)) { if (!in_array($field, $this->change)) {
$this->__set($field, isset($this->data[$field]) ? $this->data[$field] : null); $this->__set($field, isset($this->data[$field]) ? $this->data[$field] : null);
} }
} }
// 去除没有更新的字段
foreach ($this->data as $key => $val) {
if (!in_array($key, $this->change) && !$this->isPk($key) && !isset($this->relation[$key])) {
unset($this->data[$key]);
}
}
$db = self::db();
if (!empty($where)) {
$db->where($where);
}
$result = $db->update($this->data);
// 更新回调
$this->trigger('after_update', $this);
// 写入回调
$this->trigger('after_write', $this);
return $result;
} }
/** /**