From 3edc0909c07baaa4e4aec7da8bf3236059969f79 Mon Sep 17 00:00:00 2001 From: molong Date: Tue, 20 Sep 2016 22:58:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0tp=E5=86=85=E6=A0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/library/think/Model.php | 9 +++++++-- core/library/think/db/Connection.php | 11 ++++++++--- core/library/think/db/Query.php | 9 +++++++-- core/library/think/model/Merge.php | 10 +++++++++- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/core/library/think/Model.php b/core/library/think/Model.php index 30b71d52..276f4770 100644 --- a/core/library/think/Model.php +++ b/core/library/think/Model.php @@ -39,11 +39,12 @@ use think\paginator\Collection as PaginatorCollection; */ abstract class Model implements \JsonSerializable, \ArrayAccess { - // 数据库对象池 protected static $links = []; // 数据库配置 protected $connection = []; + // 数据库查询对象 + protected $query; // 当前模型名称 protected $name; // 数据表名称 @@ -147,7 +148,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $model = $this->class; if (!isset(self::$links[$model])) { // 设置当前模型 确保查询返回模型对象 - $query = Db::connect($this->connection)->model($model); + $query = Db::connect($this->connection)->model($model, $this->query); // 设置当前数据表和模型名 if (!empty($this->table)) { @@ -666,6 +667,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess if (!empty($where)) { $pk = $this->getPk(); if (is_string($pk) && isset($data[$pk])) { + if (!isset($where[$pk])) { + unset($where); + $where[$pk] = $data[$pk]; + } unset($data[$pk]); } } diff --git a/core/library/think/db/Connection.php b/core/library/think/db/Connection.php index a69f1dd6..f405f9b9 100644 --- a/core/library/think/db/Connection.php +++ b/core/library/think/db/Connection.php @@ -104,6 +104,8 @@ abstract class Connection 'sql_explain' => false, // Builder类 'builder' => '', + // Query类 + 'query' => '\\think\\db\\Query', ]; // PDO连接参数 @@ -131,12 +133,14 @@ abstract class Connection * 创建指定模型的查询对象 * @access public * @param string $model 模型类名称 + * @param string $queryClass 查询对象类名 * @return Query */ - public function model($model) + public function model($model, $queryClass = '') { if (!isset($this->query[$model])) { - $this->query[$model] = new Query($this, $model); + $class = $queryClass ?: $this->config['query']; + $this->query[$model] = new $class($this, $model); } return $this->query[$model]; } @@ -151,7 +155,8 @@ abstract class Connection public function __call($method, $args) { if (!isset($this->query['database'])) { - $this->query['database'] = new Query($this); + $class = $this->config['query']; + $this->query['database'] = new $class($this); } return call_user_func_array([$this->query['database'], $method], $args); } diff --git a/core/library/think/db/Query.php b/core/library/think/db/Query.php index 04a8184c..637b0bc5 100644 --- a/core/library/think/db/Query.php +++ b/core/library/think/db/Query.php @@ -1342,9 +1342,14 @@ class Query list($guid) = explode(' ', $tableName); if (!isset(self::$info[$guid])) { + if (!strpos($guid, '.')) { + $schema = $this->getConfig('database') . '.' . $guid; + } else { + $schema = $guid; + } // 读取缓存 - if (is_file(RUNTIME_PATH . 'schema/' . $guid . '.php')) { - $info = include RUNTIME_PATH . 'schema/' . $guid . '.php'; + if (is_file(RUNTIME_PATH . 'schema/' . $schema . '.php')) { + $info = include RUNTIME_PATH . 'schema/' . $schema . '.php'; } else { $info = $this->connection->getFields($guid); } diff --git a/core/library/think/model/Merge.php b/core/library/think/model/Merge.php index 405c6bcc..566cf077 100644 --- a/core/library/think/model/Merge.php +++ b/core/library/think/model/Merge.php @@ -233,16 +233,24 @@ class Merge extends Model if ($result) { $insertId = $db->getLastInsID($sequence); // 写入外键数据 + $pk = $this->getPk(); if ($insertId) { + if (is_string($pk)) { + $this->data[$pk] = $insertId; + } $this->data[$this->fk] = $insertId; } // 写入附表数据 + $source = $this->data; + if ($insertId && is_string($pk) && isset($source[$pk])) { + unset($source[$pk]); + } foreach (static::$relationModel as $key => $model) { $name = is_int($key) ? $model : $key; $table = is_int($key) ? $db->getTable($model) : $model; // 处理关联模型数据 - $data = $this->parseData($name, $this->data, true); + $data = $this->parseData($name, $source, true); $query = clone $db; $query->table($table)->strict(false)->insert($data); }