From 6bd6d03440eefffe34f7b114665164ee16133cfe Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 21 Dec 2016 12:14:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3Model=E7=B1=BB=E7=9A=84useGlo?= =?UTF-8?q?balScope=E6=96=B9=E6=B3=95=E6=97=A0=E6=95=88=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 80c224a3..dc7002d7 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -151,7 +151,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess if (is_null($this->autoWriteTimestamp)) { // 自动写入时间戳 - $this->autoWriteTimestamp = $this->db()->getConfig('auto_timestamp'); + $this->autoWriteTimestamp = $this->db(false)->getConfig('auto_timestamp'); } // 执行初始化操作 @@ -634,10 +634,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public function getPk($name = '') { if (!empty($name)) { - $table = $this->db()->getTable($name); - return $this->db()->getPk($table); + $table = $this->db(false)->getTable($name); + return $this->db(false)->getPk($table); } elseif (empty($this->pk)) { - $this->pk = $this->db()->getPk(); + $this->pk = $this->db(false)->getPk(); } return $this->pk; } @@ -834,7 +834,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public function allowField($field) { if (true === $field) { - $field = $this->db()->getTableInfo('', 'fields'); + $field = $this->db(false)->getTableInfo('', 'fields'); } $this->field = $field; return $this; @@ -1402,7 +1402,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 记录当前关联信息 $model = $this->parseModel($model); $name = Loader::parseName(basename(str_replace('\\', '/', $model))); - $table = $table ?: $this->db()->getTable(Loader::parseName($this->name) . '_' . $name); + $table = $table ?: $this->db(false)->getTable(Loader::parseName($this->name) . '_' . $name); $foreignKey = $foreignKey ?: $name . '_id'; $localKey = $localKey ?: Loader::parseName($this->name) . '_id'; return new BelongsToMany($this, $model, $table, $foreignKey, $localKey, $alias); @@ -1459,7 +1459,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public function __call($method, $args) { - $query = $this->db(); + if (isset(static::$db)) { + $query = static::$db; + } else { + $query = $this->db(); + } if (method_exists($this, 'scope' . $method)) { // 动态调用命名范围 $method = 'scope' . $method;