From 0fd61503a8e9b70689603652733958a06826464c Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 19 May 2016 10:48:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E6=8E=89Model=E7=B1=BB=E7=9A=84query?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=20=E9=81=BF=E5=85=8D=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E5=8C=96=E5=87=BA=E9=97=AE=E9=A2=98=20=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E4=BD=BF=E7=94=A8$this->db()=20=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 27 +++++++++++++-------------- library/think/model/Merge.php | 30 ++++++++++++++++-------------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 11ad857b..184178a1 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -34,8 +34,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected $name; // 数据表名称 protected $table; - // 查询对象 - protected $query; // 回调事件 protected static $event = []; @@ -102,12 +100,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $this->data = $data; } + // 当前类名 + $this->class = get_class($this); + if (empty($this->name)) { - $this->name = basename(str_replace('\\', '/', get_class($this))); + $this->name = basename(str_replace('\\', '/', $this->class)); } - // 当前模型的查询对象 - $this->query = $this->db(); // 执行初始化操作 $this->initialize(); } @@ -239,7 +238,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public function getPk($table = '') { if (empty($this->pk)) { - $this->pk = $this->query->getTableInfo($table, 'pk'); + $this->pk = $this->db()->getTableInfo($table, 'pk'); } return $this->pk; } @@ -335,7 +334,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } } - $result = $this->query->where($where)->update($data); + $result = $this->db()->where($where)->update($data); // 更新回调 $this->trigger('after_update', $this); @@ -352,11 +351,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return false; } - $result = $this->query->insert($this->data); + $result = $this->db()->insert($this->data); // 获取自动增长主键 if ($result && $getId) { - $insertId = $this->query->getLastInsID(); + $insertId = $this->db()->getLastInsID(); $pk = $this->getPk(); if (is_string($pk) && $insertId) { $this->data[$pk] = $insertId; @@ -448,7 +447,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return false; } - $result = $this->query->delete($this->data); + $result = $this->db()->delete($this->data); $this->trigger('after_delete', $this); return $result; @@ -882,7 +881,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public function db() { - $model = get_called_class(); + $model = $this->class; if (!isset(self::$links[$model])) { // 设置当前模型 确保查询返回模型对象 $query = Db::connect($this->connection)->model($model); @@ -905,11 +904,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess if (method_exists($this, 'scope' . $method)) { // 动态调用命名范围 $method = 'scope' . $method; - array_unshift($args, $this->query); + array_unshift($args, $this->db()); call_user_func_array([$this, $method], $args); return $this; } else { - return call_user_func_array([$this->query, $method], $args); + return call_user_func_array([$this->db(), $method], $args); } } @@ -934,7 +933,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess { if (is_null($this->fieldType)) { // 获取字段类型信息并缓存 - $this->fieldType = $this->query->getTableInfo('', 'type'); + $this->fieldType = $this->db()->getTableInfo('', 'type'); } if (is_null($value) && $this->autoWriteTimestamp && in_array($name, [$this->createTime, $this->updateTime, $this->deleteTime])) { // 自动写入的时间戳字段 diff --git a/library/think/model/Merge.php b/library/think/model/Merge.php index f21931a8..66b11c85 100644 --- a/library/think/model/Merge.php +++ b/library/think/model/Merge.php @@ -173,7 +173,8 @@ class Merge extends Model // 处理模型数据 $data = $this->parseData($this->name, $this->data); - $this->query->startTrans('merge_save_' . $this->name); + $db = $this->db(); + $db->startTrans('merge_save_' . $this->name); try { if ($this->isUpdate) { // 自动写入 @@ -184,15 +185,15 @@ class Merge extends Model } // 写入主表数据 - $result = $this->query->strict(false)->update($data); + $result = $db->strict(false)->update($data); // 写入附表数据 foreach (static::$relationModel as $key => $model) { $name = is_int($key) ? $model : $key; - $table = is_int($key) ? $this->query->getTable($model) : $model; + $table = is_int($key) ? $db->getTable($model) : $model; // 处理关联模型数据 $data = $this->parseData($name, $this->data); - $query = clone $this->query; + $query = clone $db; $query->table($table)->strict(false)->where($this->fk, $this->data[$this->getPk()])->update($data); } // 新增回调 @@ -220,10 +221,10 @@ class Merge extends Model // 写入附表数据 foreach (static::$relationModel as $key => $model) { $name = is_int($key) ? $model : $key; - $table = is_int($key) ? $this->query->getTable($model) : $model; + $table = is_int($key) ? $db->getTable($model) : $model; // 处理关联模型数据 $data = $this->parseData($name, $this->data, true); - $query = clone $this->query; + $query = clone $db; $query->table($table)->strict(false)->insert($data); } $result = $insertId; @@ -231,10 +232,10 @@ class Merge extends Model // 新增回调 $this->trigger('after_insert', $this); } - $this->query->commit('merge_save_' . $this->name); + $db->commit('merge_save_' . $this->name); return $result; } catch (\PDOException $e) { - $this->query->rollback(); + $db->rollback(); return false; } } @@ -250,25 +251,26 @@ class Merge extends Model return false; } - $this->query->startTrans('merge_delete_' . $this->name); + $db = $this->query; + $db->startTrans('merge_delete_' . $this->name); try { - $result = $this->query->delete($this->data); + $result = $db->delete($this->data); if ($result) { // 获取主键数据 $pk = $this->data[$this->getPk()]; // 删除关联数据 foreach (static::$relationModel as $key => $model) { - $table = is_int($key) ? $this->query->getTable($model) : $model; - $query = clone $this->query; + $table = is_int($key) ? $db->getTable($model) : $model; + $query = clone $db; $query->table($table)->where($this->fk, $pk)->delete(); } } $this->trigger('after_delete', $this); - $this->query->commit('merge_delete_' . $this->name); + $db->commit('merge_delete_' . $this->name); return $result; } catch (\PDOException $e) { - $this->query->rollback(); + $db->rollback(); return false; } }