From 83e3604314977d1a5f3d5130fa080308f98d8371 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 29 May 2016 11:20:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BModel=E7=B1=BB=E7=9A=84getPk?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 13 ++++++++----- library/think/db/Query.php | 8 ++++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index e468d3a8..e7fe067c 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -255,15 +255,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } /** - * 获取当前模型对象的主键 + * 获取模型对象的主键 * @access public - * @param string $table 数据表名 + * @param string $name 模型名 * @return mixed */ - public function getPk($table = '') + public function getPk($name = '') { - if (empty($this->pk)) { - $this->pk = $this->db()->getTableInfo($table, 'pk'); + if (!empty($name)) { + $table = $this->db()->getTable($name); + return $this->db()->getPk($table); + } elseif (empty($this->pk)) { + $this->pk = $this->db()->getPk(); } return $this->pk; } diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 99e2387b..6a064c6a 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -667,9 +667,9 @@ class Query $alias = $join; } $table = !empty($table) ? $table : $this->getTable($join); - if(true === $field){ + if (true === $field) { $fields = $alias . '.*'; - }else{ + } else { if (is_string($field)) { $field = explode(',', $field); } @@ -681,7 +681,7 @@ class Query $fields[] = $alias . '.' . $key . ' AS ' . $val; $this->options['map'][$val] = $alias . '.' . $key; } - } + } } $this->field($fields); if ($on) { @@ -1196,7 +1196,7 @@ class Query } /** - * 获取当前模型对象的主键 + * 获取当前数据表的主键 * @access public * @param string $table 数据表名 * @return string|array