From 77dbb1315b82bcda27f5838fd5193a5a75c1e4d8 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 19 Apr 2017 14:24:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E8=81=9A=E5=90=88=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=20=E4=BB=A5=E5=8F=8A=E6=A8=A1=E5=9E=8B=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=92=8C=E5=B1=9E=E6=80=A7=E5=90=8C=E5=90=8D=E7=9A=84?= =?UTF-8?q?BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 18 ++++++------ library/think/model/Merge.php | 55 ++++++++++++++++++++--------------- 2 files changed, 40 insertions(+), 33 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index f949a794..fd8c7124 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1083,11 +1083,14 @@ abstract class Model implements \JsonSerializable, \ArrayAccess { $relation = Loader::parseName($attr, 1, false); - if (method_exists($this, $relation) && $this->$relation() instanceof Relation) { - return $relation; - } else { - return false; + if (method_exists($this, $relation)) { + $reflect = new \ReflectionMethod($this, $relation); + if (0 == $reflect->getNumberOfParameters() && $this->$relation() instanceof Relation) { + return $relation; + } } + return false; + } /** @@ -1522,9 +1525,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public static function scope($name) { - if ($name instanceof Query) { - return $name; - } $model = new static(); $query = $model->db(); $params = func_get_args(); @@ -1542,7 +1542,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } } } - return $query; + return $model; } /** @@ -1937,7 +1937,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess array_unshift($args, $query); call_user_func_array([$model, $method], $args); - return $query; + return $model; } else { return call_user_func_array([$query, $method], $args); } diff --git a/library/think/model/Merge.php b/library/think/model/Merge.php index 01f403f8..d944979e 100644 --- a/library/think/model/Merge.php +++ b/library/think/model/Merge.php @@ -11,6 +11,7 @@ namespace think\model; +use think\Db; use think\db\Query; use think\Model; @@ -120,22 +121,19 @@ class Merge extends Model * @access public * @param string $model 模型名称 * @param array $data 数据 - * @param bool $insert 是否新增 * @return array */ - protected function parseData($model, $data, $insert = false) + protected function parseData($model, $data) { $item = []; foreach ($data as $key => $val) { - if ($insert || in_array($key, $this->change) || $this->isPk($key)) { - if ($this->fk != $key && array_key_exists($key, $this->mapFields)) { - list($name, $key) = explode('.', $this->mapFields[$key]); - if ($model == $name) { - $item[$key] = $val; - } - } else { + if ($this->fk != $key && array_key_exists($key, $this->mapFields)) { + list($name, $key) = explode('.', $this->mapFields[$key]); + if ($model == $name) { $item[$key] = $val; } + } else { + $item[$key] = $val; } } return $item; @@ -174,6 +172,11 @@ class Merge extends Model $this->setAttr($this->updateTime, null); } + // 事件回调 + if (false === $this->trigger('before_write', $this)) { + return false; + } + $db = $this->db(); $db->startTrans(); $pk = $this->getPk(); @@ -190,8 +193,16 @@ class Merge extends Model $where = $this->updateWhere; } + // 获取有更新的数据 + $data = $this->getChangedData(); + // 保留主键数据 + foreach ($this->data as $key => $val) { + if ($this->isPk($key)) { + $data[$key] = $val; + } + } // 处理模型数据 - $data = $this->parseData($this->name, $this->data); + $data = $this->parseData($this->name, $data); if (is_string($pk) && isset($data[$pk])) { if (!isset($where[$pk])) { unset($where); @@ -207,14 +218,12 @@ class Merge extends Model $name = is_int($key) ? $model : $key; $table = is_int($key) ? $db->getTable($model) : $model; // 处理关联模型数据 - $data = $this->parseData($name, $this->data); - $query = new Query; - if ($query->table($table)->strict(false)->where($this->fk, $this->data[$this->getPk()])->update($data)) { + $data = $this->parseData($name, $data); + if (Db::table($table)->strict(false)->where($this->fk, $this->data[$this->getPk()])->update($data)) { $result = 1; } } - // 清空change - $this->change = []; + // 新增回调 $this->trigger('after_update', $this); } else { @@ -231,7 +240,7 @@ class Merge extends Model } // 处理模型数据 - $data = $this->parseData($this->name, $this->data, true); + $data = $this->parseData($this->name, $this->data); // 写入主表数据 $result = $db->name($this->name)->strict(false)->insert($data); if ($result) { @@ -240,9 +249,6 @@ class Merge extends Model if ($insertId) { if (is_string($pk)) { $this->data[$pk] = $insertId; - if ($this->fk == $pk) { - $this->change[] = $pk; - } } $this->data[$this->fk] = $insertId; } @@ -256,19 +262,20 @@ class Merge extends Model $name = is_int($key) ? $model : $key; $table = is_int($key) ? $db->getTable($model) : $model; // 处理关联模型数据 - $data = $this->parseData($name, $source, true); - $query = new Query; - $query->table($table)->strict(false)->insert($data); + $data = $this->parseData($name, $source); + Db::table($table)->strict(false)->insert($data); } } // 标记为更新 $this->isUpdate = true; - // 清空change - $this->change = []; // 新增回调 $this->trigger('after_insert', $this); } $db->commit(); + // 写入回调 + $this->trigger('after_write', $this); + + $this->origin = $this->data; return $result; } catch (\Exception $e) { $db->rollback();